2010-01-15 28 views
0

我有象下面的数据:XSLT得到与逗号分隔符匹配的数据

<ProductAttributes> 
<Attribute> 
    <ItemCode>ITEM-0174676</ItemCode> 
    <AttributeCode>host_interface</AttributeCode> 
    <AttributeDescription>Host Interface</AttributeDescription> 
    <AttributeValueCode>usb</AttributeValueCode> 
    <AttributeValueDescription>USB</AttributeValueDescription> 
    <GroupCode>technical_information</GroupCode> 
    <GroupDescription>Technical Information</GroupDescription> 
    <GroupPostion /> 
    <DisplayInList>True</DisplayInList> 
    <GroupPosition>1</GroupPosition> 
    </Attribute> 
<Attribute> 
    <ItemCode>ITEM-0174676</ItemCode> 
    <AttributeCode>host_interface</AttributeCode> 
    <AttributeDescription>Host Interface</AttributeDescription> 
    <AttributeValueCode /> 
    <AttributeValueDescription>USB</AttributeValueDescription> 
    <GroupCode>technical_information</GroupCode> 
    <GroupDescription>Technical Information</GroupDescription> 
    <GroupPostion /> 
    <DisplayInList>True</DisplayInList> 
    <GroupPosition>1</GroupPosition> 
    </Attribute> 
<Attribute> 

在这里,在上面的XML <AttributeDescription>由具有在这种情况下,既<Attribute>节点相同的文字我想显示的reslut如下这将使用<AttributeValueDescription>节点,因此结果将是

主机接口:USB,USB

所以对于结果的任何帮助吗?

由于提前, 嗡

+0

你需要检测如果这些值是重复的? – 2010-01-15 09:36:26

回答

2

我想你想的HTML作为输出。您需要按<ItemCode>, <AttributeCode>将数据分组。这意味着复合Muenchian分组方法。你需要这把钥匙:

<xsl:key 
    name="AttributeByAttributeCode" 
    match="Attribute" 
    use="concat(ItemCode, '|', AttributeCode)" 
/> 

然后,您可以通过<AttributeCode>使用的关键,组内的每个<ProductAttributes>

<xsl:template match="ProductAttributes"> 
    <!-- check every attribute… --> 
    <xsl:for-each select="Attribute"> 

    <!-- …select all attributes that share the same item and attribute codes --> 
    <xsl:variable name="EqualAttributes" select=" 
     key('AttributeByAttributeCode', concat(ItemCode, '|', AttributeCode)) 
    " /> 

    <!-- make sure output is generated for the first of them only --> 
    <xsl:if test="generate-id() = generate-id($EqualAttributes[1])"> 
     <div> 
     <xsl:value-of select="AttributeDescription" /> 
     <xsl:text>: </xsl:text> 
     <!-- now make a list out of any attributes that are equal --> 
     <xsl:apply-templates mode="list" select=" 
      $EqualAttributes/AttributeValueDescription 
     " /> 
     </div> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:template> 

<!-- generic template to make a comma-separated list out of input elements --> 
<xsl:template match="*" mode="list"> 
    <xsl:value-of select="." /> 
    <xsl:if test="position() &lt; last()"> 
    <xsl:text>, </xsl:text> 
    </xsl:if> 
</xsl:template> 

以上会导致

<div>Host Interface: USB, USB</div> 
+0

非常感谢你Tomalak它的工作! 谢谢。 – 2010-01-15 12:36:04

+0

很高兴提供帮助。 :) P.S .:如果您在这方面没有进一步的问题,您可以将答案标记为已接受。 – Tomalak 2010-01-15 12:55:00

+0

我在这个博客是新的是有一些链接或按钮标记为'答案接受'。 – 2010-01-15 14:15:22