Xml xpath Syntax "go"

Source: Internet
Author: User
Tags xml xpath xpath

 

 

[Original address: http://www.cnblogs.com/hya1109/archive/2007/12/16/996535.html]

I have also sent a message about. net, but it is not very detailed. Now I will introduce in detail how to operate XML files in C #, just like learning to operate a database and learn the SQL language, before learning to operate XML and the language, we need to familiarize ourselves with the XML "SQL" Statement XPath. Since this series of posts does not aim to introduce the XPath syntax in detail, I borrowed leves from the garden post to briefly introduce the XPath Syntax:

XPath is the XML query language, which is similar to SQL. The XML below is used as an example to introduce the XPath syntax.

 
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="USA">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <price>9.90</price> 
  </cd>
</catalog>
 
         

Locate a node
XML is a tree structure, similar to the structure of the data folder in the file system. XPath is similar to the path naming method in the file system. However, XPath is a pattern that selects all nodes in the XML file whose path conforms to a pattern. For example, to select all the price elements in CD under catalog, you can use:

 
/catalog/cd/price     
 

If the beginning of xpath is a slash (/), it indicates that this is an absolute path. If there are two diagonal lines (//) at the beginning, all elements in the file that match the pattern will be selected, even at different layers in the tree. The following syntax Selects all elements in the file called Cd (all layers in the tree will be selected ):

 
//cd
 

Select unknown element
Use the asterisk (wildcards, *) to select unknown elements. The following syntax Selects all child elements of/CATALOG/CD:

 
/catalog/cd/*
 

The following syntax Selects all child elements of catalog, including price as the child element.

 
/catalog/*/price
 

The following syntax Selects all elements with two parent nodes called Price.

 
/*/*/price
 

The following syntax Selects all elements in the file.

 
//*
 

Note that to access non-hierarchical elements, the XPath syntax must start with two diagonal lines (//). To access unknown elements, use the asterisk (*), an asterisk can only represent an element with an unknown name, but cannot represent an element with an unknown level.

Select Branch
Use brackets to select branch. The following syntax extracts the first element named cd from the child element of catalog. The XPath definition does not contain the 0th element.

 
/catalog/cd[1]
 

The following syntax selects the last cd element in catalog: (XPathj does not define the first () function. In the preceding example, [1] can be used to retrieve the first element.

 
/catalog/cd[last()]
 

The following syntax Selects all/catalog/cd elements containing the price sub-element.

 
/catalog/cd[price]
 

The following syntax Selects all/catalog/cd elements whose price value is 10.90.

 
/catalog/cd[price=10.90]
 

The following syntax selects the price element of all/catalog/cd elements whose value is equal to 10.90.

 
/catalog/cd[price=10.90]/price
 

Select more than one path
You can select more than one path by using the Or operand (|. For example:

 
/catalog/cd/title | catalog/cd/artist
 

Select All title and artist Elements

 
//title | //artist
 

Select All title, artist, and price elements

 
//title | //artist | //price
 

Select attributes
In XPath, you can select attributes in addition to elements. All attributes start. For example, select all the attributes in the file called country:

 
//@country
         

Select all cd elements containing the country attribute:

 
//cd[@country]
         

The following syntax Selects all cd elements containing attributes

 
//cd[@*]
         

The following syntax selects the cd element whose country attribute value is UK.

 
//cd[@country='UK']

Once you have mastered the xpath syntax, You can theoretically access any node and any value in the xml file.

 

 

[Original address: http://www.cnblogs.com/hya1109/archive/2007/12/16/996535.html]

I have also sent a message about. net, but it is not very detailed. Now I will introduce in detail how to operate XML files in c #, just like learning to operate a database and learn the SQL language, before learning to operate xml and the language, we need to familiarize ourselves with the xml "SQL" Statement xpath. Since this series of posts does not aim to introduce the xpath syntax in detail, I borrowed leves from the garden post to briefly introduce the xpath Syntax:

XPath is the XML query language, which is similar to SQL. The XML below is used as an example to introduce the XPath syntax.

 
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="USA">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <price>9.90</price> 
  </cd>
</catalog>
 
         

Locate a node
XML is a tree structure, similar to the structure of the data folder in the file system. XPath is similar to the path naming method in the file system. However, XPath is a Pattern that selects all nodes in the XML file whose path conforms to a Pattern. For example, to select all the price elements in cd under catalog, you can use:

 
/catalog/cd/price     
 

If the beginning of XPath is a slash (/), it indicates that this is an absolute path. If there are two diagonal lines (//) at the beginning, all elements in the file that match the pattern will be selected, even at different layers in the tree. The following syntax Selects all elements in the file called cd (all layers in the tree will be selected ):

 
//cd
 

Select unknown element
Use the asterisk (Wildcards, *) to select unknown elements. The following syntax Selects all child elements of/catalog/cd:

 
/catalog/cd/*
 

The following syntax Selects all child elements of catalog, including price as the child element.

 
/catalog/*/price
 

The following syntax Selects all elements with two parent nodes called price.

 
/*/*/price
 

The following syntax Selects all elements in the file.

 
//*
 

Note that to access non-hierarchical elements, the XPath syntax must start with two diagonal lines (//). To access unknown elements, use the asterisk (*), an asterisk can only represent an element with an unknown name, but cannot represent an element with an unknown level.

Select Branch
Use brackets to select branch. The following syntax extracts the first element named cd from the child element of catalog. The XPath definition does not contain the 0th element.

 
/catalog/cd[1]
 

The following syntax selects the last cd element in catalog: (XPathj does not define the first () function. In the preceding example, [1] can be used to retrieve the first element.

 
/catalog/cd[last()]
 

The following syntax Selects all/CATALOG/CD elements containing the price sub-element.

 
/catalog/cd[price]
 

The following syntax Selects all/CATALOG/CD elements whose price value is 10.90.

 
/catalog/cd[price=10.90]
 

The following syntax selects the price element of all/CATALOG/CD elements whose value is equal to 10.90.

 
/catalog/cd[price=10.90]/price
 

Select more than one path
You can select more than one path by using the or operand (|. For example:

 
/catalog/cd/title | catalog/cd/artist
 

Select All title and artist Elements

 
//title | //artist
 

Select All title, artist, and price elements

 
//title | //artist | //price
 

Select attributes
In XPath, you can select attributes in addition to elements. All attributes start. For example, select all the attributes in the file called country:

 
//@country
         

Select all CD elements containing the country attribute:

 
//cd[@country]
         

The following syntax Selects all CD elements containing attributes

 
//cd[@*]
         

The following syntax selects the CD element whose country attribute value is UK.

 
//cd[@country='UK']

Once you have mastered the XPath syntax, You can theoretically access any node and any value in the XML file.

Related Article

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.