我有一个.xml文件,其中包含两组信息,我想根据它们的ID值(1.2)相互匹配。例如,这个片段。
<rule id="1.2">
<checker id="checker.id">
<description locale="en">description</description>
</checker>
</rule>
<rule id="1.2">
<checker>
<category locale="en">Advisory</category>
<decidable locale="en">Yes</decidable>
</checker>
</rule> 我对每个规则都有一个.xsl,将这些值添加到表条目中
<row>
<entry>
<xsl:value-of select="@id"/>
</entry>
<entry>
<xsl:for-each select="checker">
<xsl:value-of select="category[@locale=$locale]"/>
</xsl:for-each>
</entry>
<entry>
<xsl:for-each select="checker">
<xsl:value-of select="decidable[@locale=$locale]"/>
</xsl:for-each>
</entry>
<entry>
<xsl:for-each select="checker">
<p>
<codeph>
<xsl:value-of select="@id"/></codeph><xsl:text> </xsl:text>
<xsl:value-of select="description[@locale=$locale]"/>
</p>
</xsl:for-each>
</entry>
</row> 当前的结果给出了这个结果,但是它已经创建了两个单独的行,尽管ID是相同的。如何才能使ID相同的信息在同一行中?
<row>
<entry>1.2</entry>
<entry>Advisory</entry>
<entry>Yes</entry>
<entry>
<p>
<codeph/></p>
</entry>
</row>
<row>
<entry>1.2</entry>
<entry/>
<entry/>
<entry>
<p>
<codeph>checker.id</codeph>description</p>
</entry>
</row>预期结果:
<row>
<entry>1.2</entry>
<entry>Advisory</entry>
<entry>Yes</entry>
<entry>
<p><codeph>checker.id</codeph>description</p>
</entry>
</row>发布于 2017-03-31 19:58:41
这是所谓Muenchian grouping的变体.
下面的XSLT选择所有<rule>s的排序<rule>的第一个匹配,然后通过将//运算符与谓词相匹配,将所有<rule>与相同的@id匹配,从而编译所需的结果。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="kSorted" match="rule" use="@id" />
<xsl:variable name="locale" select="'en'" />
<xsl:template match="/root">
<xsl:apply-templates select="rule[generate-id() = generate-id(key('kSorted',@id)[1])]">
<xsl:sort select="@id" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="rule">
<xsl:variable name="thisID" select="@id" />
<row>
<entry><xsl:value-of select="@id"/></entry>
<entry><xsl:value-of select="//rule[@id = $thisID]//category[@locale=$locale]"/></entry>
<entry><xsl:value-of select="//rule[@id = $thisID]//decidable[@locale=$locale]"/></entry>
<entry>
<p>
<codeph><xsl:value-of select="checker/@id"/></codeph><xsl:text> </xsl:text>
<xsl:value-of select="checker/*[@locale=$locale][1]"/>
</p>
</entry>
</row>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/43143836
复制相似问题