1

我在 Muenchian grouping XSLT 1.0 Group By上找到了这篇文章,并完成了我想做的事情,但我不知道如何对我的子节点进行分组。

我的 XML 看起来有点像这样:

<NewDataSet>
   <Vehicle>
     <ManufacturerId>53</ManufacturerId>
     <ManufacturerName>VAUXHALL</ManufacturerName>
     <Model>Corsa</Model>
   </vehicle>
   <Vehicle>
     <ManufacturerId>53</ManufacturerId>
     <ManufacturerName>VAUXHALL</ManufacturerName>
     <Model>Astra</Model>
   </vehicle>
   <Vehicle>
     <ManufacturerId>53</ManufacturerId>
     <ManufacturerName>VAUXHALL</ManufacturerName>
     <Model>Corsa</Model>
   </vehicle>
   <Vehicle>
     <ManufacturerId>54</ManufacturerId>
     <ManufacturerName>FORD</ManufacturerName>
     <Model>KA</Model>
   </vehicle>
   <Vehicle>
     <ManufacturerId>54</ManufacturerId>
     <ManufacturerName>FORD</ManufacturerName>
     <Model>Focus</Model>
   </vehicle>
   <Vehicle>
     <ManufacturerId>54</ManufacturerId>
     <ManufacturerName>FORD</ManufacturerName>
     <Model>KA</Model>
   </vehicle>
   <Vehicle>
     <ManufacturerId>55</ManufacturerId>
     <ManufacturerName>CITROEN</ManufacturerName>
     <Model>C4</Model>
   </vehicle>
<NewDataSet>

这是我到目前为止的代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" omit-xml-declaration="yes" />
   <xsl:key name="groups" match="/NewDataSet/Vehicle" use="ManufacturerName" />
   <xsl:template match="/NewDataSet">
      <xsl:apply-templates select="Vehicle[generate-id() = generate-id(key('groups', ManufacturerName)[1])]"/>
   </xsl:template>
   <xsl:template match="Vehicle">
     <div class="makeLnk">
     <a href="/stocklist/?ManufacturerId={ManufacturerId}"><xsl:value-of select="ManufacturerName"/></a>
    <xsl:for-each select="key('groups', ManufacturerName)">
      <div class="modelLnk"><xsl:value-of select="Model"/></div>
    </xsl:for-each>
    </div>
   </xsl:template>
</xsl:stylesheet>

这将制造商名称组合在一起并列出所有模型。但我现在也想对模型进行分组,从而删除重复的名称。但无法解决语法。

将不胜感激任何帮助。

谢谢。

4

1 回答 1

1

使用由 的ManufacturerNameVehicle的值组成的第二个键Model

<xsl:key name="model" match="Vehicle/Model" use="concat(../ManufacturerName, '|', .)"/>

然后替换

   <xsl:template match="Vehicle">
     <div class="makeLnk">
     <a href="/stocklist/?ManufacturerId={ManufacturerId}"><xsl:value-of select="ManufacturerName"/></a>
    <xsl:for-each select="key('groups', ManufacturerName)">
      <div class="modelLnk"><xsl:value-of select="Model"/></div>
    </xsl:for-each>
    </div>
   </xsl:template>

   <xsl:template match="Vehicle">
     <div class="makeLnk">
     <a href="/stocklist/?ManufacturerId={ManufacturerId}"><xsl:value-of select="ManufacturerName"/></a>
    <xsl:for-each select="key('groups', ManufacturerName)/Model[generate-id() = 
  generate-id(key('model', concat(../ManufacturerName, '|', .))[1])]">
      <div class="modelLnk"><xsl:value-of select="."/></div>
    </xsl:for-each>
    </div>
   </xsl:template>
于 2012-12-19T14:49:31.513 回答