我在Muenchian上找到了这篇文章,将XSLT 1.0组分组,并与我想做的事情一部分,但是我无法弄清楚如何分组我的子节点。<<<<<<<<<<<<<<<<<</p>
我的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>
这将制造商组合在一起,并列出了下面的所有模型。但是我现在也想对模型进行分组,从而删除重复的名称。但是无法弄清楚语法。
感谢任何帮助。
谢谢。
使用由Vehicle
的ManufacturerName
组成的第二个键和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>