3
我尝试检索一个元素的子元素的属性值列表,但我希望这些值只出现一次。在XSLT中选择与每个元素相关的断点
举例来说,我有以下XML
<root>
<sec>
<nom-title>
<nom-chapter>
<nom-article><data att="1.1"/></nom-article>
<nom-article>
<nom-item><data att="1.1"/></nom-item>
<nom-item><data att="1.2"/></nom-item>
</nom-article>
</nom-chapter>
<nom-chapter>
<nom-article><data att="2.1"/></nom-article>
<nom-article><data att="1.1"/></nom-article>
</nom-chapter>
</nom-title>
<nom-title>
<nom-chapter>
<nom-article><data att="1.1"/></nom-article>
</nom-chapter>
</nom-title>
</sec>
</root>
而且我想这样的结果:
<root>
<nom-title>
<att>1.1</att>
<att>1.2</att>
<att>2.1</att>
<nom-chapter>
<att>1.1</att>
<att>1.2</att>
<nom-article>
<att>1.1</att>
</nom-article>
<nom-article>
<att>1.1</att>
<att>1.2</att>
<nom-item><att>1.1</att></nom-item>
<nom-item><att>1.2</att></nom-item>
</nom-article>
</nom-chapter>
</nom-title>
<nom-title>
<att>1.1</att>
<nom-chapter>
<att>1.1</att>
<nom-article>
<att>1.1</att>
</nom-article>
</nom-chapter>
</nom-title>
</root>
我试图使用xsl:关键元素,但它只返回一个元素的值。在这个例子中,第一个标题只返回1.1,而不是第二个。我使用了xsl:
<xsl:key name="allAtt"
match="//*[starts-with(name(.),'nom-')]/data"
use="@att"/>
<xsl:template match="nom-title|nom-chapter|nom-article|nom-item">
<xsl:element name="name(.)">
<xsl:apply-templates select=".//*[starts-with(name(.),'nom-')]/data
</xsl:element>
</xsl:template>
<xsl:template match="data">
<xsl:variable name="att" select="@att"/>
<xsl:if test="generate-id(.)=generate-id(key('allAtt',$att)[1]">
<xsl:element name="att"><xsl:value-of select="$att"></xsl:element>
</xsl:if>
</xsl:template>
根据我的经验,xslt需要有效的xml开始。它看起来像“nom-article”元素没有关闭。可能想尝试首先解决这个问题。 – 2012-04-04 10:46:54
我同意Justin的观点,请首先向我们展示一个格式良好的XML输入样本,目前输入的结构并不清晰,有几个标签没有正确关闭(例如'',' ')。 –
2012-04-04 10:53:09
对不起,错误的复制粘贴,我已修复它。 – claudex 2012-04-04 11:06:23