Xpath:从 id 属性与 id 锚的匹配中找到一个元素值 [英] Xpath: find an element value from a match of id attribute to id anchor
问题描述
我想找到与 id 属性匹配的元素的值,我只有 ref - 带有 # 的位,锚点.
I would like to find the value of an element matched on id attribute for which I only have the ref - the bit with #, the anchor.
我正在寻找partyId的值:
I am looking for the value of partyId:
< party id="partyA" >
< partyId >THEID< /partyId >
但是为了到达那里我只有以下的href
but to get there I only have the href from the following
< MyData >
< MyReference href="#partyA" />
去掉# 符号对我来说不好看.
Strip the # sign does not look good to me.
有什么提示吗?
推荐答案
因为你没有提供完整的 XML 文档,我不得不使用 //
—— 我强烈建议避免这种做法.
Because you haven't provided complete XML documents, I have to use //
-- a practice I strongly recommend to avoid.
假设
$vDataRef
定义为
//MyData/MyReference/@href
并且它的字符串值是"#partyA"
,那么一种可能的选择所需节点的XPath表达式是:
and its string value is "#partyA"
, then one possible XPath expression that selects the wanted node is:
//party[@id=substring($vDataRef,2)]
如果 XML 文档有一个 DTD,其中 party
的 id
属性被定义为 ID
类型,那么它是使用标准XPath函数更方便、更高效id()
:
In case the XML document has a DTD in which the id
attribute of party
is defined to be of type ID
, then it is more convenient and efficient to use the standard XPath function id()
:
id(substring($vDataRef,2))
这篇关于Xpath:从 id 属性与 id 锚的匹配中找到一个元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!