我想获取以下 xml 中唯一的“id”字段。
<jsonObject>
<headers>
<Resources>
<field1>test-profile order 11</field1>
<id>abca7923-eaea-4e9c-a952-b4aadc39a295</id>
<description1>This is a test profile</description1>
...
</Resources>
<Resources>
<field1>test-profile order 11</field1>
<id>abca7923-eaea-4e9c-a952-b4aadc39a295</id>
<description1>This is a test profile</description1>
...
</Resources>
<Resources>
<field1>test-profile order 11</field1>
<id>abca7923-eaea-4e9c-a952-b4aadc39a296</id>
<description1>This is a test profile</description1>
...
</Resources>
<Resources>
<field1>test-profile order 11</field1>
<id>abca7923-eaea-4e9c-a952-b4aadc39a296</id>
<description1>This is a test profile</description1>
...
</Resources>
</headers>
</jsonObject>
我尝试使用下面的 xslt 获取唯一的“id”。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="profileGroup" match="Resources" use="profileId" />
<xsl:template match="/jsonObject/headers">
<jsonObject>
<total>
<xsl:for-each select="//id[not(.=preceding::id)]">
<xsl:value-of select="count(//id[.=current()])" />
</xsl:for-each>
</total>
</jsonObject>
</xsl:template>
</xsl:stylesheet>
但这不起作用。我得到了这样的回应。
<total>22</total>
但我期待这个回应
<total>2</total>
我的 xslt 有什么问题?
如果您的处理器支持 XSLT 2.0 或更高版本,您可以简单地执行以下操作:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/jsonObject">
<jsonObject>
<total>
<xsl:value-of select="count(distinct-values(headers/Resources/id))" />
</total>
</jsonObject>
</xsl:template>
</xsl:stylesheet>