XPATH: Selecting multiple records with Wildcard Namespaces

ⅰ亾dé卋堺 提交于 2019-12-05 13:35:06

Try this:

//*[local-name() = 'element']/*[local-name() = 'A' and (. = 4 or . = 5)]
    /../*[local-name() = 'B']

This expression selects Value 2 and Value 3.

The proper way of treating namespaces is to register them with your XPath API and to ...use them in the XPath expression:

Use:

//x:element[x:A = 4 or x:A = 5]/x:B

XSLT - based verification:

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

 <xsl:template match="/">
     <xsl:copy-of select="//x:element[x:A = 4 or x:A = 5]/x:B"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document (the provided fragment, wrapped by a single top element that has a default namespace definition):

<t xmlns="some:default NS">
    <element>
        <A>3</A>
        <B>Value 1</B>
    </element>
    <element>
        <A>4</A>
        <B>Value 2</B>
    </element>
    <element>
        <A>5</A>
        <B>Value 3</B>
    </element>
</t>

the XPath expression is evaluated and the result (selected nodes) of this evaluation is copied to the output:

<B xmlns="some:default NS">Value 2</B>
<B xmlns="some:default NS">Value 3</B>

You can nest predicates.

//*[
  local-name() = 'element' and *[local-name() = 'A' and (. = 4 or . = 5)]
]/*[
  local-name() = 'B'
]/text()

But in reality you should be using namespace-aware XPath:

//ns1:element[ns2:A[. = 4 or . = 3]]/ns2:B/text()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!