2010-01-15 30 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