XSLT:两个文件之间的 XPath 比较
我想通过 XSLT 比较两个 XML 文件。如果文档 1 中特定类型的所有元素都位于文档 2 中的相同 XPath 位置,则应认为比较成功。
请考虑
<entry>
<entry1>
<entry2>
<value type="1"/>
</entry2>
</entry1>
</entry>
文档 1。
观察的元素是“value”(属性 type=1),其中位于入口/入口1/入口2。因此,在这种意义上的比较
<entry>
<entry0/>
<entry0/>
<entry1>
<entry2>
<value type="1"/>
</entry2>
</entry1>
</entry>
应被视为成功,而
<entry>
<entry1>
<value type="1"/>
</entry1>
</entry>
则不成功,因为“值”(属性类型=1)位于条目/条目1。还 与 的比较
<entry>
<entry1>
<entry2>
<value type="2"/>
</entry2>
</entry1>
</entry>
应被视为不成功,因为 value 的属性为 type=2。
我在 XSLT 中完成此任务的天真尝试类似于:
<xsl:template match="value">
<xsl:if test="not(document($doc2)/.[@type=@type])">
<xsl:text>something is missing</xsl:text>
</xsl:if>
</xsl:template>
这种方法并不成功,因为在第二个文档中选择所需的 XPath 似乎不起作用。
也许您知道如何解决这个问题?
马特
I would like to compare two XML-files via XSLT. The comparison should be considered to be successful if all elements of a specific type in document 1 are located at the same XPath position in document 2.
Consider
<entry>
<entry1>
<entry2>
<value type="1"/>
</entry2>
</entry1>
</entry>
as document 1.
The element under observation is "value" (with attribute type=1) which is located at entry/entry1/entry2. Therefore a comparison in this sense to
<entry>
<entry0/>
<entry0/>
<entry1>
<entry2>
<value type="1"/>
</entry2>
</entry1>
</entry>
should be considered as successful, while
<entry>
<entry1>
<value type="1"/>
</entry1>
</entry>
is not successful, since "value" (with attribute type=1) is located at entry/entry1. Also
the comparison to
<entry>
<entry1>
<entry2>
<value type="2"/>
</entry2>
</entry1>
</entry>
should be considered as not successful since the attribute of value is type=2.
My naive trial to fulfill this task in XSLT was something like:
<xsl:template match="value">
<xsl:if test="not(document($doc2)/.[@type=@type])">
<xsl:text>something is missing</xsl:text>
</xsl:if>
</xsl:template>
This approach wasn't successful because the selection of the desired XPath within the 2nd document seems not to work.
Maybe you have an idea on how to address this question?
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题实在是太不明确了。例如,除了要求 doc1 中的每个元素在 doc2 中都有对应的元素之外,是否还要求 doc2 中的每个元素在 doc1 中都有对应的元素?
然而,接近的情况可能是条件“对于
D1
中的每个元素V1
,使得name(V1)=N
,存在D2
中的元素V2
,使得name(V2)=N 且 deep-equal(V1, V2) 且 path(V1) = path(V2)< /code>,其中 path($V) 定义为
string-join($V/ancestor-or-self::*/name())
",这会转换为以下 XPath 2.0 表达式:Your question is hopelessly underspecified. For example, as well as requiring every element in doc1 to have a corresponding element in doc2, do you also require every element in doc2 to have a corresponding element in doc1?
However, something close might be the condition "for every element
V1
inD1
such thatname(V1)=N
, there exists an elementV2
inD2
such thatname(V2)=N and deep-equal(V1, V2) and path(V1) = path(V2)
, where path($V) is defined asstring-join($V/ancestor-or-self::*/name())
", Which translates into the following XPath 2.0 expression:只是为了好玩,Kay 博士的答案的 XSLT 1.0 翻译:
那么
$vTest1 = ''
将是测试的布尔值。Just for fun, an XSLT 1.0 translation of Dr. Kay's answer:
Then
$vTest1 = ''
would be the boolean value of the test.