2012-02-17 60 views
1

下面是我的XML如何使用xsl在一行中对节点组进行排序?

<products> 
    <product> 
     <item>Pen</item> 
     <price>10</price> 
    </product> 
    <product> 
     <item>Pencil</item> 
     <price>20</price> 
    </product> 
    <product> 
     <item>Bag</item> 
     <price>25</price> 
    </product> 
</products> 

我需要输出类似下面

product_name  price  remark 
Pen    10  Pen+Pencil+Bag 
Pencil    20  Pen+Pencil+Bag 
Bag    25  Pen+Pencil+Bag 

我如何在XSLT 1.0

回答

0

这种转变remark为组:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:variable name="vSortedNames"> 
    <xsl:call-template name="sortedItemList"/> 
</xsl:variable> 

<xsl:template match="/*"> 
    <xsl:text>product_name&#9;price&#9;remark</xsl:text> 

    <xsl:apply-templates select="*"> 
    <xsl:sort select="price" data-type="number"/> 
    </xsl:apply-templates> 
</xsl:template> 

<xsl:template match="product"> 
    <xsl:value-of select= 
    "concat('&#xA;', item, '&#9;', price, '&#9;', $vSortedNames)"/> 
</xsl:template> 

<xsl:template name="sortedItemList"> 
    <xsl:for-each select="/*/product"> 
    <xsl:sort select="price" data-type="number"/> 
    <xsl:if test="not(position() = 1)">+</xsl:if> 
    <xsl:value-of select="item"/> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

当所提供的XML文档施加:

<products> 
    <product> 
     <item>Pen</item> 
     <price>10</price> 
    </product> 
    <product> 
     <item>Pencil</item> 
     <price>20</price> 
    </product> 
    <product> 
     <item>Bag</item> 
     <price>25</price> 
    </product> 
</products> 

产生想要的,正确的结果

product_name price remark 
Pen 10 Pen+Pencil+Bag 
Pencil 20 Pen+Pencil+Bag 
Bag 25 Pen+Pencil+Bag