XPath multi-condition Query

Source: Internet
Author: User

I am used to the simple use of xpath. I have studied it when I encounter multiple condition query requirements.
Post the implementation method for your reference:

There is such an XML:
<? XML version =/"1.0/" encoding =/"ISO-8859-1/"?>
<Test>

<Cell> <data type = "string"> alpha </data> </cell>
<Cell> <data type = "Number"> 100 </data> </cell>
<Cell> <data type = "Number"> 200 </data> </cell>
<Cell> <data type = "Boolean"> true </data> </cell>
</Test>

All cell nodes whose data nodes meet the requirement of type = string and inner text = alpha

XPath: // cell [DATA [text () = 'alpha'] and data [@ type = 'string']
Or // cell [DATA [text () = 'alpha' and @ type = 'string']
Analysis: // cell indicates searching all cell nodes
[] It is a condition. Only cell nodes that meet this condition can be searched out.
Data [text () = 'alpha' and @ type = 'string'] subnodes with such (innertext = Alpha and type = string) can be searched out.
Connect multiple conditions with and

What if I add a node?
<? XML version =/"1.0/" encoding =/"ISO-8859-1/"?>
<Test>
<Row>

<Cell> <data type = "string"> alpha </data> </cell>
<Cell> <data type = "Number"> 100 </data> </cell>
<Cell> <data type = "Number"> 200 </data> </cell>
<Cell> <data type = "Boolean"> true </data> </cell>

</Row>
<Row>

<Cell> <data type = "string1"> alpha </data> </cell>
<Cell> <data type = "number1"> 100 </data> </cell>
<Cell> <data type = "number1"> 200 </data> </cell>
<Cell> <data type = "boolean1"> true </data> </cell>

</Row>
</Test>

Find all row nodes whose sun tzu node data meets type = string and inner text = Alpha.

XPath:
// Row [cell/data [text () = 'alpha'] and cell/data [@ type = 'string']
Or
// Row [cell/data [text () = 'alpha' and @ type = 'string']

Verification Program:

String xmlpayload = "<? XML version =/"1.0/" encoding =/"ISO-8859-1/"?> "+
"<Test>" +
@ "<Row>" +
"<Cell> <data type =/" String/"> alpha </data> </cell>" +
"<Cells> <data type =/" number/"> 100 </data> </cell>" +
"<Cells> <data type =/" number/"> 200 </data> </cell>" +
"<Cell> <data type =/" Boolean/"> </data> </cell>" +
"</Row>" +
"<Row>" +
"<Cell> <data type =/" String/"> gamma </data> </cell>" +
"<Cell> <data type =/" number/"> 12 </data> </cell>" +
"<Cell> <data type =/" number/"> 25 </data> </cell>" +
"<Cell> <data type =/" Boolean/"> 1 </data> </cell>" +
"</Row>" +
"</Test> ";
Xmldocument document = new xmldocument ();
Document. loadxml (xmlpayload );
String xmlpath = "// row [cell/data [text () = '1'] and cell/data [@ type = 'boolean']";
Xmlnodelist nodelist = Document. selectnodes (xmlpath );
Console. writeline ("nodelist. Count:" + nodelist. Count );
Console. Readline ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.