Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Advanced XPath Query, please help!

Posted on 2006-05-22
6
Medium Priority
?
1,172 Views
Last Modified: 2008-01-09
I have the following xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<raml xmlns="raml20.xsd">
   <cmData>
      <header>
         <log dateTime="2006-05-21T18:08:17" action="created" appInfo="ActualExporter" />
      </header>
      <managedObject class="ADJG" distName="WBTS-4003/WCEL-400315/ADJG-6">
         <p name="AdjgBCC">0</p>
         <p name="AdjgBCCH">512</p>
         <p name="AdjgCI">20037</p>
      </managedObject>
      <managedObject class="ADJG" distName="WBTS-3049/WCEL-304912/ADJG-11">
         <p name="AdjgBCC">7</p>
         <p name="AdjgBCCH">114</p>
         <p name="AdjgCI">10491</p>
      </managedObject>
      <managedObject class="ADJG" distName="WBTS-3050/WCEL-305013/ADJG-11">
         <p name="AdjgBCC">1</p>
         <p name="AdjgBCCH">111</p>
         <p name="AdjgCI">10112</p>
      </managedObject>
      .
      .
      .
      .
   </cmData>
</raml>

I have the following two strings variables:
Source_CI = 304912 (for example)
and
Target_CI = 10491 (for example)

What is the XPath query that can I use to select the first node that matches the following conditions:
1) The node name should be "managedObject"
2) The value of the attribute "class" of that node should be equals to "ADJG"
3) The value of the attribute "distName" of that node should contains (not equal) the value of "Source_ID" variable
4) The value/InnerText of the child "p" node (this child "p" node should has a "name" attribute with value of "AdjgCI") of that node should be equals to the value of the "Target_CI" variable

For the example above, the XPath query should select the second "managedObject" node, which is:
<managedObject class="ADJG" distName="WBTS-3049/WCEL-304912/ADJG-11">
   <p name="AdjgBCC">7</p>
   <p name="AdjgBCCH">114</p>
   <p name="AdjgCI">10491</p>
</managedObject>


For more details:
I am using C# .NET 2.0, and I am planning to call the method XmlNode.SelectSingleNode(String XPath). But I don't know really what the XPath should be equal on my case.

I don't know really If this could be done in one XPath query, but I am really new in this issue and any help would be appreciated.


Salan..
0
Comment
Question by:salan_alani
  • 3
  • 3
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16736546
Hi salan_alani,

this can be done with the following XPath query

//managedObject[@class='ADJG'  and contains(@distName, '3049') and (p[@name = 'AdjgCI'] = '10491')] [1]

you can build this as a string... so you can first create this string by concatenating the variables in it

that would be something like
"//managedObject[@class='ADJG'  and contains(@distName, '" + Source_CI + ...

also note that you have a namespace, so you should bind this namespace in the namespaceManager
you can bind it to the default namespace as it is in the XML ... ""
or you can give a prefix, eg. "x" but than your XPath should change like this

//x:managedObject[@class='ADJG'  and contains(@distName, '3049') and (x:p[@name = 'AdjgCI'] = '10491')] [1]


Cheers!
0
 
LVL 2

Author Comment

by:salan_alani
ID: 16737232
I am always getting a null XmlNode when calling the XmlNode.SelectSingleNode
I did not yet know how to bind the XmlNameSpaceManager

My code like this:

XmlDocument XD = new XmlDocument();
XD.Load("MySource.xml");

XmlNamespaceManager XNM = new XmlNamespaceManager(XD.NameTable);
XNM.AddNamespace(string.Empty, XD.DocumentElement.NamespaceURI);            

XmlNode XN;
// for simplicity (this should take the first managedObject node)
XN = XD.DocumentElement.SelectSingleNode("cmData/managedObject", XNM);
if (XN != null)
    MessageBox.Show(XN.OuterXml);
else
    MessageBox.Show("Error");


But I am always getting the Error message


Salan..
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 16737333

> XN = XD.DocumentElement.SelectSingleNode("cmData/managedObject", XNM);
SelectSingleNode accepts one Node... so make sure it is only one
you would also need a "//" in front of the XPath
try this
XN = XD.DocumentElement.SelectSingleNode("//cmData/managedObject[1]", XNM);

if that doesn't work,
let's hard-code it in first

XmlNamespaceManager XNM = new XmlNamespaceManager(XD.NameTable);
XNM.AddNamespace("x", "raml20.xsd");
...
XN = XD.DocumentElement.SelectSingleNode("//x:cmData/x:managedObject[1]", XNM);


     
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:salan_alani
ID: 16738284
Thanks Gertone,

It works fine.. really appreciate your help..

One last question:
I want to make an attribute with a DateTime value like the below format:
2006-05-21T17:28:48

Is there a built-in method to change the DateTime.Now to a format like above, or I need to hard-code it by my self.


Salan..
0
 
LVL 2

Author Comment

by:salan_alani
ID: 16738334
Nevermind, I figured out how to do formatting to DateTime variable.

Thanks again for your help Gertone..

Salan..
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16738348
welcome
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question