0

我想通过使用属性对我的 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>
4

1 回答 1

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>
于 2013-08-30T05:35:41.620 回答