XSL:两个 xml 文档的浅比较?
如何匹配 XSLT 中的 XML 内容?我将为您提供输入 XML 文件和预期输出以及我尝试过的内容:
input1.xml
<?xml version="1.0" ?>
<list1>
<value>1</value>
<value>2</value>
<value>3</value>
</list1>
input2.xml (经过编辑以显示顺序不重要)
<?xml version="1.0" ?>
<list2>
<value>3</value>
<value>1</value>
</list2>
所需输出(经过编辑以显示顺序不重要):
1 match
2 no match
3 match
或其任何排列。
我尝试过的:
<xsl:variable name="list1" select="document('./resources/input1.xml')"/>
<xsl:variable name="list2" select="document('./resources/input2.xml')"/>
<xsl:for-each select="$list1/list1/value">
<xsl:variable name="check"/>
<xsl:variable name="list1_value" select="."/>
<xsl:for-each select="$list2/list2/value">
<xsl:if test="$list1_value=.">
<xsl:variable name="check" select="1"/>
</xsl:if>
</xsl:for-each>
<xsl:choose>
<xsl:when test="$check='1'">
<xsl:value-of select="."/> match<br />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/> no match<br />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
当然这不起作用,因为它尝试使用变量作为变量:)
此外,如何将较大的列表与应用于较小列表的 XSLT 相匹配,如下所示:
XSLT 应用于 XML:
<?xml version="1.0" ?>
<list2>
<value>1</value>
<value>2</value>
</list2>
并且该列表作为变量导入:
<?xml version="1.0" ?>
<list1>
<value>1</value>
<value>2</value>
<value>3</value>
</list1>
相同的期望结果。谢谢你!
How do I match XML content in XSLT? I'll give you the input XML files and expected output as well as what I've tried:
input1.xml
<?xml version="1.0" ?>
<list1>
<value>1</value>
<value>2</value>
<value>3</value>
</list1>
input2.xml (edited to show that order should not matter)
<?xml version="1.0" ?>
<list2>
<value>3</value>
<value>1</value>
</list2>
desired output (edited to show that order should not matter):
1 match
2 no match
3 match
Or any permutation thereof.
What I tried:
<xsl:variable name="list1" select="document('./resources/input1.xml')"/>
<xsl:variable name="list2" select="document('./resources/input2.xml')"/>
<xsl:for-each select="$list1/list1/value">
<xsl:variable name="check"/>
<xsl:variable name="list1_value" select="."/>
<xsl:for-each select="$list2/list2/value">
<xsl:if test="$list1_value=.">
<xsl:variable name="check" select="1"/>
</xsl:if>
</xsl:for-each>
<xsl:choose>
<xsl:when test="$check='1'">
<xsl:value-of select="."/> match<br />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/> no match<br />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
Of course this doesn't work because it tries to use a variable as a variable :)
Additionally, how would you match the larger list to an XSLT applied to a smaller list as such:
XSLT is applied to XML:
<?xml version="1.0" ?>
<list2>
<value>1</value>
<value>2</value>
</list2>
And this list is imported as a variable:
<?xml version="1.0" ?>
<list1>
<value>1</value>
<value>2</value>
<value>3</value>
</list1>
Same desired result. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此转换:
当应用于第一个提供的 XML 文档时:
产生所需的正确结果:
更新:相同的转换,但使用两个不同的 XML 文件:
Update2:当要比较的较短元素列表是源 XML 文档时,OP 另外需要一个解决方案:
当此转换应用于此源时XML 文档:
再次产生了想要的正确结果:
更新3:现在OP进行了第三次修改:两个列表中元素的顺序不一样问题...
解决方案更简单:
This transformation:
when applied on the first provided XML document:
produces the wanted, correct result:
Update: The same transformation, but with two different XML files:
Update2: The OP additionally wants a solution when the shorter lists of elements to compare is the source XML document:
when this transformation is applied on this source XML document:
again the wanted, correct result is produced:
Update3: Now a third modification by the OP: The order of elements in the two lists do not matter...
The solution is even simpler: