为什么这个 XPath 查询不像我假设的那样工作? (搜索带有 @ 的元素,同时排除带有其他 @ 的元素)
Why doesn't this XPath query work as I assumed? (searching for elements with some @ while excluding those with other @)
我有以下查询 (XPath 2.0):
//xref[contains(@href,'#') and @class='- topic/xref ' and @type!='step' and @type!='fig' and @type!='substep']
如您所见,我想找到 topic/xref 个在其 href 属性中带有散列的元素。我想排除 ceratin 类型的元素。
问题是,上面的查询不显示带有 @outputclass='expandable'
的元素
我不得不 运行 一个单独的来识别它们:
//xref[contains(@href,'#') and @outputclass='expandable']
为什么第一个较长的查询不显示那些元素?我也试过 contains(@class='- topic/xref ) 而不是 @class=' - topic/xref ' 但它没有帮助。
尝试以下 XPath
:
//xref[@class="- topic/xref " and contains(@href, "#") and not(@type=("fig", "substep", "table") or @outputclass="expandable")]
这将 return xref
元素在 href
属性中具有 class="- topic/xref "
和 #
但不具有具有值的 type
属性"fig", "substep", "table"
或 outputclass
属性值为 "expandable"
您没有显示 XML 来源,但我怀疑您希望检索没有 @type
属性的元素。但是,条件 @type!='XYZ'
仅对具有 @type
属性且其值不是 'XYZ' 的元素为真。如果我的猜测是正确的,你打算 not(@type='XYZ')
.
我有以下查询 (XPath 2.0):
//xref[contains(@href,'#') and @class='- topic/xref ' and @type!='step' and @type!='fig' and @type!='substep']
如您所见,我想找到 topic/xref 个在其 href 属性中带有散列的元素。我想排除 ceratin 类型的元素。 问题是,上面的查询不显示带有 @outputclass='expandable'
的元素我不得不 运行 一个单独的来识别它们:
//xref[contains(@href,'#') and @outputclass='expandable']
为什么第一个较长的查询不显示那些元素?我也试过 contains(@class='- topic/xref ) 而不是 @class=' - topic/xref ' 但它没有帮助。
尝试以下 XPath
:
//xref[@class="- topic/xref " and contains(@href, "#") and not(@type=("fig", "substep", "table") or @outputclass="expandable")]
这将 return xref
元素在 href
属性中具有 class="- topic/xref "
和 #
但不具有具有值的 type
属性"fig", "substep", "table"
或 outputclass
属性值为 "expandable"
您没有显示 XML 来源,但我怀疑您希望检索没有 @type
属性的元素。但是,条件 @type!='XYZ'
仅对具有 @type
属性且其值不是 'XYZ' 的元素为真。如果我的猜测是正确的,你打算 not(@type='XYZ')
.