您在这里需要一个连接键,因为您想要不同的compartmentCode
上下文中的值configurationCode
,所以你的密钥看起来像这样
<xsl:key name="queries"
match="record"
use="concat(column[@name='configurationCode'], '|', column[@name='compartmentCode'])"/>
然后,在一个query
,要获取唯一的隔间代码值,请执行以下操作...
<xsl:for-each
select="queryResults/record[generate-id() = generate-id(key('queries', concat(column[@name='configurationCode'], '|', column[@name='compartmentCode']))[1])]">
试试这个(注意我如何将一些代码分离到模板匹配中query
以避免过多的代码嵌套,并使一些 xpath 更简单)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="queries" match="record" use="concat(column[@name='configurationCode'], '|', column[@name='compartmentCode'])"/>
<xsl:template match="/">
<root>
<xsl:for-each select="//trailer">
<xsl:choose>
<xsl:when test="trailer_tag='0'">
<xsl:copy>
<!--copy trailer node-->
<xsl:copy-of select="@*|node()"/>
<xsl:apply-templates select="//root/output/queries/query[parameters/parameter[@name='id'] = current()/id]" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<!--something else-->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template match="query">
<configurations>
<configuration>
<id>
<xsl:value-of select="queryResults/record/column[@name='configurationCode']"/>
</id>
<compartments>
<!--I need to build the following structure for each unique compartmentCode-->
<xsl:for-each select="queryResults/record[generate-id() = generate-id(key('queries', concat(column[@name='configurationCode'], '|', column[@name='compartmentCode']))[1])]">
<compartment>
<code>
<xsl:value-of select="column[@name='compartmentCode']"/>
</code>
<capacities>
<xsl:for-each select="key('queries', concat(column[@name='configurationCode'], '|', column[@name='compartmentCode']))">
<capacity>
<unit>
<xsl:value-of select="column[@name='unitName']"/>
</unit>
<val>
<xsl:value-of select="column[@name='CompartmentCapacity']"/>
</val>
</capacity>
</xsl:for-each>
</capacities>
</compartment>
</xsl:for-each>
</compartments>
</configuration>
</configurations>
</xsl:template>
</xsl:stylesheet>