Help with XPath Expressions

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

Greetings,

I'm searching an XML file but am having some trouble with the XPath syntax.

Below is the general format of the XML file ("[...]" represents additional
data). I need to first locate the vendor with a specific code value. I then
need to locate the product under that vendor with a specific ID value.

Perhaps someone good at XPath could help me out.

Thanks!

<ProductFeed>
<Info>
[...]
</Info>
<Filters>
[...]
</Filters>
<Vendors>
<Vendor>
<Code>[...]</Code>
[...]
<Products>
<ID>[...]</ID>
[...]
</Product>
<Product>
<ID>[...]</ID>
[...]
</Product>
</vendor>
<vendor>
<Code>[...]</Code>
[...]
<Products>
<ID>[...]</ID>
[...]
</Product>
<Product>
<ID>[...]</ID>
[...]
</Product>
</vendor>
</Vendors>
</ProductFeed>

<<<<<

Jonathan
 
Jonathan said:
I'm searching an XML file but am having some trouble with the XPath syntax.
[...]
Perhaps someone good at XPath could help me out.

If you have an XPath question, you really should post it in a forum
where XPath questions are on-topic.

In the meantime, check this out:
http://www.w3.org/TR/xpath/

Pete
 
I'm searching an XML file but am having some trouble with the XPath syntax.

Below is the general format of the XML file ("[...]" represents
additional data). I need to first locate the vendor with a specific code
value. I then need to locate the product under that vendor with a
specific ID value.

Perhaps someone good at XPath could help me out.
<ProductFeed>
<Info>
[...]
</Info>
<Filters>
[...]
</Filters>
<Vendors>
<Vendor>
<Code>[...]</Code>
[...]
<Products>
<ID>[...]</ID>
[...]
</Product>
<Product>
<ID>[...]</ID>
[...]
</Product>
</vendor>
<vendor>
<Code>[...]</Code>
[...]
<Products>
<ID>[...]</ID>
[...]
</Product>
<Product>
<ID>[...]</ID>
[...]
</Product>
</vendor>
</Vendors>
</ProductFeed>

Try:

/ProductFeed/Vendors/Vendor
Code:
/Product[ID='bar']

Arne
 
Greetings,

I'm searching an XML file but am having some trouble with the XPath syntax.

Below is the general format of the XML file ("[...]" represents additional
data). I need to first locate the vendor with a specific code value. I then
need to locate the product under that vendor with a specific ID value.

Perhaps someone good at XPath could help me out.

Thanks!

I used the following XML:

<?xml version="1.0" encoding="utf-8"?>
<ProductFeed>
<Info>
</Info>
<Filters>
</Filters>
<Vendors>
<Vendor>
<Code>123</Code>
<Name>Magic Monkey Outlet</Name>
<Products>
<Product>
<ID>B234</ID>
<ProductName>Flying Monkey</ProductName>
</Product>
<Product>
<ID>A235</ID>
<ProductName>Golden Monkey</ProductName>
</Product>
</Products>
</Vendor>
<Vendor>
<Code>124</Code>
<Name>Candy Cane Super Store</Name>
<Products>
<Product>
<ID>B234</ID>
<ProductName>Cinimon Candy Cane</ProductName>
</Product>
<Product>
<ID>B235</ID>
<ProductName>Cherry Candy Cane</ProductName>
</Product>
</Products>
</Vendor>
</Vendors>
</ProductFeed>


Here are two ways one using XPath to find the producr B234 for vendor = 123, product B234

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ConsoleApplication65
{
class Program
{
static void Main ( string[] args )
{

// using xpath
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.Load ( "examplexml.xml" );

XmlNamespaceManager nsMgr = new XmlNamespaceManager ( xmlDoc.NameTable );

XmlNodeList selectedNodes = xmlDoc.SelectNodes (
"//Product[child::ID[text()=\"B234\"] and ancestor::Vendor/Code/text() = \"123\"]",
nsMgr );
foreach ( XmlNode selectedNode in selectedNodes )
{
Console.WriteLine ( selectedNode.InnerXml );
}

}
}
}

Output:
<ID>B234</ID><ProductName>Flying Monkey</ProductName>

Here is using Linq-to-XML:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Linq;

namespace ConsoleApplication65
{
class Program
{
static void Main ( string[] args )
{



XElement xmlDoc = XElement.Load ( "examplexml.xml" );
var products = from product in xmlDoc.Descendants ( "Product" )
where product.Element ( "ID" ).Value == "B234" &&
product.XPathSelectElement("ancestor::Vendor/Code").Value == "123" select product;

foreach ( XElement product in products )
Console.WriteLine ( product );
}
}
}
 
Got it. Thanks!

Jonathan

Arne Vajhøj said:
I'm searching an XML file but am having some trouble with the XPath
syntax.

Below is the general format of the XML file ("[...]" represents
additional data). I need to first locate the vendor with a specific code
value. I then need to locate the product under that vendor with a
specific ID value.

Perhaps someone good at XPath could help me out.
<ProductFeed>
<Info>
[...]
</Info>
<Filters>
[...]
</Filters>
<Vendors>
<Vendor>
<Code>[...]</Code>
[...]
<Products>
<ID>[...]</ID>
[...]
</Product>
<Product>
<ID>[...]</ID>
[...]
</Product>
</vendor>
<vendor>
<Code>[...]</Code>
[...]
<Products>
<ID>[...]</ID>
[...]
</Product>
<Product>
<ID>[...]</ID>
[...]
</Product>
</vendor>
</Vendors>
</ProductFeed>

Try:

/ProductFeed/Vendors/Vendor
Code:
/Product[ID='bar']

Arne[/QUOTE]
 
Thanks. I got this working.

Jonathan

Tom Shelton said:
Greetings,

I'm searching an XML file but am having some trouble with the XPath
syntax.

Below is the general format of the XML file ("[...]" represents
additional
data). I need to first locate the vendor with a specific code value. I
then
need to locate the product under that vendor with a specific ID value.

Perhaps someone good at XPath could help me out.

Thanks!

I used the following XML:

<?xml version="1.0" encoding="utf-8"?>
<ProductFeed>
<Info>
</Info>
<Filters>
</Filters>
<Vendors>
<Vendor>
<Code>123</Code>
<Name>Magic Monkey Outlet</Name>
<Products>
<Product>
<ID>B234</ID>
<ProductName>Flying Monkey</ProductName>
</Product>
<Product>
<ID>A235</ID>
<ProductName>Golden Monkey</ProductName>
</Product>
</Products>
</Vendor>
<Vendor>
<Code>124</Code>
<Name>Candy Cane Super Store</Name>
<Products>
<Product>
<ID>B234</ID>
<ProductName>Cinimon Candy Cane</ProductName>
</Product>
<Product>
<ID>B235</ID>
<ProductName>Cherry Candy Cane</ProductName>
</Product>
</Products>
</Vendor>
</Vendors>
</ProductFeed>


Here are two ways one using XPath to find the producr B234 for vendor =
123, product B234

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ConsoleApplication65
{
class Program
{
static void Main ( string[] args )
{

// using xpath
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.Load ( "examplexml.xml" );

XmlNamespaceManager nsMgr = new XmlNamespaceManager (
xmlDoc.NameTable );

XmlNodeList selectedNodes = xmlDoc.SelectNodes (
"//Product[child::ID[text()=\"B234\"] and ancestor::Vendor/Code/text() =
\"123\"]",
nsMgr );
foreach ( XmlNode selectedNode in selectedNodes )
{
Console.WriteLine ( selectedNode.InnerXml );
}

}
}
}

Output:
<ID>B234</ID><ProductName>Flying Monkey</ProductName>

Here is using Linq-to-XML:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Linq;

namespace ConsoleApplication65
{
class Program
{
static void Main ( string[] args )
{



XElement xmlDoc = XElement.Load ( "examplexml.xml" );
var products = from product in xmlDoc.Descendants ( "Product" )
where product.Element ( "ID" ).Value == "B234" &&
product.XPathSelectElement("ancestor::Vendor/Code").Value == "123" select
product;

foreach ( XElement product in products )
Console.WriteLine ( product );
}
}
}
 
Back
Top