【发布时间】:2020-09-03 09:10:57
【问题描述】:
我如何显示students/student/name 那些存在于<studentIds> 节点中的students/student/name。
这里是xml节点参考-
<?xml version="1.0" encoding="UTF-8"?>
<test>
<studentIds>
<id><![CDATA[123]]></id>
<id><![CDATA[126]]></id>
</studentIds>
<students>
<student>
<id><![CDATA[123]]></id>
<name><![CDATA[Goku]]></name>
</student>
<student>
<id><![CDATA[124]]></id>
<name><![CDATA[Luffy]]></name>
</student>
<student>
<id><![CDATA[126]]></id>
<name><![CDATA[Naruto]]></name>
</student>
</students>
</test>
到目前为止,我已经找到了这个解决方案 - 创建一个值为 <studentIds> 的变量,然后执行 contains() -
<xsl:variable name="sid">
<xsl:for-each select="test/studentIds/value">
<xsl:value-of select="."/>
<xsl:if test="position() != last()"> </xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="test/students/student">
<xsl:if test="contains($sid, id)">
<xsl:apply-templates select="name"/> 
</xsl:if>
</xsl:for-each>
但我相信一定有比这个更好的解决方案。
【问题讨论】: