PowerShell Tip: Querying an XML file using XPath syntax

Source: Internet
Author: User
Tags trims xpath

"TechTarget Chinese original"

XML is a good way to store structured data, but it can be difficult to make data work in it. Each language has a specific way to query the namespaces, elements, and attributes in the XML file. PowerShell is no exception. PowerShell is slightly different when querying an XML file, and the technique relies heavily on the Select-xml command and the XPath syntax.

XPath is a language that defines the composition of an XML file. XPath has existed since 1999 and has been used to query the most standard methods of XML files.

XPath defines the XML file as a tree. Each node in the XML file has an interaction with the other nodes, thus forming a genealogy. If one of the nodes contains many other nodes, then the node is the "parent" in the genealogy, and the other node is the "child" in the genealogy. In the same vein, the child node is a "sibling" relationship. Our decomposition of nodes in this way is to make it easier for everyone to understand the structure of the XML file.

For example, the XML file contains inventory details for the car dealer, which looks like this:

<?xml version= "1.0" encoding= "Utf-8"?>
<dealership>
<cars>
<car make= "Ford" model= "Taurus" year= ">"
<trims>
<trimpackage>SportEdition</trimpackage>
<trimpackage>Basic</trimpackage>
</trims>
</car>
<car make= "BMW" model= "328i" year= ">"
<trims>
<trimpackage>SportEdition</trimpackage>
<trimpackage>Basic</trimpackage>
</trims>
</car>
</cars>
</dealership>

This XML file represents the reseller. The dealer is a node, and the dealer "inside" is a node that represents the onboard inventory. The dealer is the "parent" of the car node, and the car node is the "child". Then there are branches underneath the car node that produce the "Kids" node. Each vehicle node forms a "sibling" relationship. In this example, there is a tree-multi-attribute relationship. property enables the intrinsic characteristics of a particular node. You'll notice that each car has its own logo and model properties. This approach allows the vehicle type to be more specific. These are the properties of the specified car node.

How to query XML using XPath

The XML structure above, however, how to query the structure using the XPath language? XPath is a powerful, but complex, query syntax. To simplify the operation, let's talk about the general use of XPath and try to find all the in-vehicle nodes in this file. XPath provides a variety of ways to find information about XML files. Which method to use depends primarily on what kind of effect you want to get. For our team, for example, we have the distributor total node, which contains many in-vehicle sub-nodes, and the car node contains many child nodes. This family relationship can be expressed in XPath syntax:

/dealership/cars/car

Through this special query method can get all the car sub-nodes, belong to the Reseller node branch, and these reseller nodes belong to the XML file root directory. This is a different approach. However, there are other ways to focus on the car nodes. If you don't want to know the whole method, then you just need to enter the query//car to find all the in-vehicle nodes in the XML file. There is nothing special about this approach, but it will be used a few times when we need it.

Using the Select-xml command

Now, let's analyze what's special about PowerShell, and point out how select-xml works. Select-xml is a PowerShell command and is used to query the System.Xml.XmlDocument object. The command will have a "partner" called XPath, which is used to describe the XPath string for a particular XmlDocument object. Let's take a look at the example.

First, in order to be able to use the select-xml, we must have XmlDocument objects available for querying. Just need to read the XML folder in the existing not working computer. Then we need to read the XML file from the system file and convert it to the XmlDocument object. Use the get-content command to read a plain text file in an XML folder and convert it to a variable, as shown in 1

Figure 1: Using the get-content command to read an XML file from the folder system

After you run the command, you'll see the basetype array, but it doesn't work. Because it must be XmlDocument form. In order to convert the xmlcontent variable to a XmlDocument object, we also need to turn the variable into a different object type. After doing this, use the XML type accelerator, shown in 2.

Figure 2. Convert the BaseType sequence to XmlDocument using the XML type accelerator

Now you can see that $xmlcontents is classified as the XmlDocument class. We are ready to use Select-xml now.

The best way to use Select-xml is to pass the XML file through the pipeline and use XPath as a parameter, as shown in 3.

Figure 3. Reading an XML file using the Select-xml and XPath parameters

Earlier, I used the same XPath query method and returned to two on-board nodes, which is exactly what I was looking for. I can also use the//car shortcut to get the same results.

Find deeper nodes

What if you want to learn more about car details? You need to expand each node. Use the Select-object command, or simply select some other command, and then implement the query function through Expandproperty, as shown in 4. This approach requires reading each node, and it needs to read the specific nodes that each node contains.

Figure 4. To expand a node using the Expandproperty parameter

Suppose you just need to know the specifics of the Ford Taurus? Because "Ford" and "Taurus" are XML attributes, the XPath approach complicates it:

$xmlContents | Select-xml-xpath "//car[@make = ' Ford ']" | Select-expandproperty node

I added the @make= ' Ford ' string to the XPath syntax. This is a way to query XML properties. Remembering this syntax will help to understand the substitution of the "attribute" symbol.

More XPath information

If you want to learn more about XPath syntax, I recommend that you visit the XPath tutorial on w3schools.com website. And if you want to learn more about the Select-xml command, we recommend petri.com on the article titled Search XML Files with PowerShell using Select-xml.

PowerShell Tip: Querying an XML file using XPath syntax

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.