gpt4 book ai didi

xslt - 跨XSLT节点集的不同值

转载 作者:行者123 更新时间:2023-12-03 16:45:55 24 4
gpt4 key购买 nike

再次关于节点集中的不同节点(基于属性值)。
假设您具有以下结构:

<struct>
<a>
<x id="1">a_1</x>
<x id="2">a_2</x>
<x id="3">a_3</x>
</a>
<b inherits="a">
<x id="2">b_2</x>
</b>
</struct>


<struct/>可能包含多个像 <b/>一样继承相同 <a/>的元素。同时允许多个元素,例如 <a/><a/><b/>的顺序是任意的。继承是单层的。

问题:如何创建一个单个XPath来为给定的 <b/>选择以下节点集:

<x id="1">a_1</x>
<x id="2">b_2</x>
<x id="3">a_3</x>


请注意第二行上的 b_2值。

有什么解决办法吗?

更新:

重新开始的XPath应该具有以下格式: b[(magic_xpath)[@id=2]='b_2'],其中 magic_xpath<x/><a/>中选择不同的 <b/>

现实生活中的 <struct/>看起来可能像这样:

<struct>
<a>
<x id="1">a_1</x>
<x id="2">a_2</x>
<x id="3">a_3</x>
</a>
<b inherits="a">
<x id="2">I don't match resulting XPath</x>
</b>
<b inherits="a">
<x id="2">b_2</x>
</b>
</struct>

最佳答案

用:

  $vB/x
|
/*/*[name() = $vB/@inherits]
/x[not(@id = $vB/x/@id)]


其中 $vB定义为 b元素。

这将选择所有 b/x元素和名称为以下内容的 x元素( id的子代)的所有 b/x/@id子元素(具有不等于任何 struct/*属性的 struct属性的联合)的并集。 b/@inherits的值。

或者,按照OP在注释中的要求-不带变量:

  /*/b/x
|
/*/*[name() = /*/b/@inherits]
/x[not(@id = /*/b/x/@id)]


完整的基于XSLT的验证:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:copy-of select=
"/*/b/x
|
/*/*[name() = /*/b/@inherits]
/x[not(@id = /*/b/x/@id)]

"/>
</xsl:template>
</xsl:stylesheet>


当此转换应用于提供的(更正为格式正确的)XML文档时:

<struct>
<a>
<x id="1">a_1</x>
<x id="2">a_2</x>
<x id="3">a_3</x>
</a>
<b inherits="a">
<x id="2">b_2</x>
</b>
</struct>


将评估单个XPath表达式并输出所选节点:

<x id="1">a_1</x>
<x id="3">a_3</x>
<x id="2">b_2</x>

关于xslt - 跨XSLT节点集的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com