XPath - 根据相同条件从不同路径检索多个值

时间:2022-02-04 15:39:13

This is my XML

这是我的XML

<root>
   <entry id="1">
      <value name="ID">11</value>
      <value name="ENABLE">0</value>
      <value name="NAME">USER1</value>
   </entry>
   <entry id="2">
      <value name="ID">11</value>
      <value name="ENABLE">0</value>
      <value name="NAME">USER2</value>
   </entry>
   <entry id="3">
      <value name="ID">12</value>
      <value name="ENABLE">0</value>
      <value name="NAME">USER1</value>
   </entry>
</root>

I need an XPath query to retrieve all the names whose id is 11.

我需要一个XPath查询来检索id为11的所有名称。

I tried:

我试过了:

String xpath = "/root/entry/value[@name=ID/text()='11']/value[@name='NAME']";
String xpath = "/root/entry/value[@name=ID/text()='11']/NAME";

Can someone tell me what is wrong in my query?

有人能告诉我我的查询有什么问题吗?

EDIT:

编辑:

Expected Output:

预期产出:

USER1
USER2

both these have ID = 11

这两个都有ID = 11

5 个解决方案

#1


0  

This would be the correct path:

这将是正确的路径:

/root/entry[value[@name = 'ID'] = 11]/value[@name = 'NAME']

To retrieve a list of nodes, you need to use the version of xPath.evaluate() that takes a returnType parameter:

要检索节点列表,您需要使用带有returnType参数的xPath.evaluate()版本:

InputSource document = new InputSource(new FileInputStream("user1.xml"));
String xpath = "/root/entry[value[@name = 'ID'] = 11]/value[@name = 'NAME']";
NodeSet result = (NodeSet)xPath.evaluate(xpath, document, XPathConstants.NODESET); 
for(int index = 0; index < result.getLength(); index ++) {      
    Node node = result.item(index);
    String name = node.getNodeValue();
}

#2


3  

/root/entry[value[@name="ID" and .=11]]/value[@name="NAME"]

or

要么

/root/entry/value[@name="ID" and .=11]/../value[@name="NAME"]

or

要么

/descendant::entry[value=11 and value/@name="ID"]/value[@name="NAME"]

or

要么

//value[.=11]/@name[.="ID"]/ancestor::entry/value[@name="NAME"]

or many other variations

或许多其他变化

#3


1  

Use:

使用:

/*/*[*[@name='ID'] = 11]/*[@name='NAME']

XSLT - based verification:

基于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="/*/*[*[@name='ID'] = 11]/*[@name='NAME']"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

当此转换应用于提供的XML文档时:

<root>
    <entry id="1">
        <value name="ID">11</value>
        <value name="ENABLE">0</value>
        <value name="NAME">USER1</value>
    </entry>
    <entry id="2">
        <value name="ID">11</value>
        <value name="ENABLE">0</value>
        <value name="NAME">USER2</value>
    </entry>
    <entry id="3">
        <value name="ID">12</value>
        <value name="ENABLE">0</value>
        <value name="NAME">USER1</value>
    </entry>
</root>

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

评估XPath表达式,并将此评估的结果(所有选定节点)复制到输出:

<value name="NAME">USER1</value>
<value name="NAME">USER2</value>

#4


0  

try this,

尝试这个,

String xpath = "/root/entry[value[@name='ID' and text()='11']]/value[@name="NAME"]";

String xpath =“/ root / entry [value [@ name ='ID'和text()='11']] / value [@ name =”NAME“]”;

#5


0  

Use:

使用:

//entry[value[@name = 'ID'] = 11]/value[@name = 'NAME']

#1


0  

This would be the correct path:

这将是正确的路径:

/root/entry[value[@name = 'ID'] = 11]/value[@name = 'NAME']

To retrieve a list of nodes, you need to use the version of xPath.evaluate() that takes a returnType parameter:

要检索节点列表,您需要使用带有returnType参数的xPath.evaluate()版本:

InputSource document = new InputSource(new FileInputStream("user1.xml"));
String xpath = "/root/entry[value[@name = 'ID'] = 11]/value[@name = 'NAME']";
NodeSet result = (NodeSet)xPath.evaluate(xpath, document, XPathConstants.NODESET); 
for(int index = 0; index < result.getLength(); index ++) {      
    Node node = result.item(index);
    String name = node.getNodeValue();
}

#2


3  

/root/entry[value[@name="ID" and .=11]]/value[@name="NAME"]

or

要么

/root/entry/value[@name="ID" and .=11]/../value[@name="NAME"]

or

要么

/descendant::entry[value=11 and value/@name="ID"]/value[@name="NAME"]

or

要么

//value[.=11]/@name[.="ID"]/ancestor::entry/value[@name="NAME"]

or many other variations

或许多其他变化

#3


1  

Use:

使用:

/*/*[*[@name='ID'] = 11]/*[@name='NAME']

XSLT - based verification:

基于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="/*/*[*[@name='ID'] = 11]/*[@name='NAME']"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

当此转换应用于提供的XML文档时:

<root>
    <entry id="1">
        <value name="ID">11</value>
        <value name="ENABLE">0</value>
        <value name="NAME">USER1</value>
    </entry>
    <entry id="2">
        <value name="ID">11</value>
        <value name="ENABLE">0</value>
        <value name="NAME">USER2</value>
    </entry>
    <entry id="3">
        <value name="ID">12</value>
        <value name="ENABLE">0</value>
        <value name="NAME">USER1</value>
    </entry>
</root>

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

评估XPath表达式,并将此评估的结果(所有选定节点)复制到输出:

<value name="NAME">USER1</value>
<value name="NAME">USER2</value>

#4


0  

try this,

尝试这个,

String xpath = "/root/entry[value[@name='ID' and text()='11']]/value[@name="NAME"]";

String xpath =“/ root / entry [value [@ name ='ID'和text()='11']] / value [@ name =”NAME“]”;

#5


0  

Use:

使用:

//entry[value[@name = 'ID'] = 11]/value[@name = 'NAME']