XML属性的XSL组

时间:2013-08-30 03:29:03

标签: xslt attributes

我想通过使用属性对我的xml条目进行分组来创建列表。 这是我的xml:

<Content>
    <contenItem ProductType="Breakfast" name="Eggs" />
    <contenItem ProductType="Breakfast" name="Bacon" />
    <contenItem ProductType="Lunch" name="Fish" />
    <contenItem ProductType="Dinner" name="Steak" />
</Content>

我正在尝试获得此结果,但无法弄清楚如何

   <ul>
     <li>Breakfast
        <ul>
           <li>Eggs</li>
           <li>Bacon</li>
        </ul>
      </li>
       <li>Lunch
        <ul>
           <li>Fish</li>
        </ul>
      </li>
      <li>Dinner
        <ul>
           <li>Steak</li>
        </ul>
      </li>
    </ul>

1 个答案:

答案 0 :(得分:0)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="about:legacy-compat" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:key name="kProductType" match="contenItem" use="@ProductType" />  

<xsl:template match="@*|node()">
 <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="/*">
 <ul>
   <xsl:apply-templates select="contenItem[
   generate-id() = generate-id(key('kProductType',@ProductType)[1])]" />
 </ul>
</xsl:template>

<xsl:template match="contenItem">
 <li>
   <xsl:value-of select="@ProductType" />
   <ul>
     <xsl:apply-templates select="key('kProductType',@ProductType)" mode="sublist"/>
   </ul>  
 </li>
</xsl:template>

<xsl:template match="contenItem" mode="sublist">
 <li>
   <xsl:value-of select="@name" />
 </li>
</xsl:template>


</xsl:stylesheet>