这是我的XML
<?xml version='1.0'?>
<root>
<elems>
<status>Completed</status>
<description>Records</description>
<sub_type>SEC</sub_type>
<type>FIR</type>
<result>Done</result>
</elems>
<elems>
<status>Completed</status>
<description>Records</description>
<sub_type>SEC</sub_type>
<type>FIR</type>
<result>Done</result>
</elems>
<elems>
<status>Completed</status>
<description>Morals</description>
<sub_type>GIX</sub_type>
<type>PANC</type>
<result>Done</result>
</elems>
<elems>
<status>Completed</status>
<description>Morals</description>
<sub_type>GIX</sub_type>
<type>PANC</type>
<result>Done</result>
</elems>
<elems>
<status>Completed</status>
<description>Checking</description>
<type>SSO</type>
<result>Done</result>
</elems>
</root>
我想要一个变量,它应该通过连接type
和sub_type
元素来给出唯一的值(因为elems
元素是重复和重复的元素(,如下所示。sub_type
可以是null元素。对于下面的示例,变量名称可以是vars
。。
FIRSEC PANCGIX SSO
然后我应该能够使用这个$var
进行foreach
<root>
<xsl:for-each select="$vars">
<inner><xsl:value-of select="." /></inner>
</xsl:for-each>
<root>
它应该打印
<root>
<inner>FIRSEC</inner>
<inner>PANCGIX</inner>
<inner>SSO</inner>
</root>
在XSLT2.0中,使用distinct-values()
函数是非常琐碎的
<xsl:variable name="vars" select="distinct-values(/root/elems/concat(type,sub_type))"/>
注意,变量是冗余的,因为您可以直接执行:
<root>
<xsl:for-each select="distinct-values(/root/elems/concat(type,sub_type))">
<inner>
<xsl:value-of select="." />
</inner>
</xsl:for-each>
</root>