I have a XML that looks like this:
<?xml version="1.0"?>
<RootName>
<RandomNode v="someValue"/>
<Series>
<Idendity v="C16"/>
<CodeOut v="C41073"/>
<Period>
<TimePeriod v="2013-07-18T22:00Z/2013-07-19T22:00Z"/>
<Resolution v="PT60M"/>
<Interval>
<Pos v="1"/>
<Qty v="14.1"/>
</Interval>
<Interval>
<Pos v="2"/>
<Qty v="20.7"/>
</Interval>
And I need a xPath that returns all the Period nodes which matches these conditions:
- The node
CodeOut/CodeInhas the value of any of the values that I have in an array - This node
CodeOutcan be namedCodeOutorCodeIn, but only one of these - The date on
TimePeriodmust match
The only node that repeates over the xml is the Series node. In other words, there is only one Period per Series, but there is a lot of different Series.
For example, get all Period nodes which have his Codeout or CodeIn value to being C41073 or B85028 and the date being 2013-07-18.
I tried, to match the multiple names, using something like:
//*[@v="C41073"] | //*[@v="B85028"] | ...
But I think it will be better if only matches the correct nodes, just in case some other node has the same value, isn't it?
I was searching to use something like "contains", but it works in a different way.
I'm using .Net, if that matters, and I'm going to use this xPath on the .SelectNodes() function.
EDIT:
Something strange is happening. Maybe the syntax is not correct. Look at this tests:
This: doc.SelectNodes("/*")(0).Name is returning RootName
This: doc.SelectNodes("/*/*").Count is returning 912
This: doc.SelectNodes("/*/*")(11).Name is returning Series
But this: doc.SelectNodes("/RootName").Count is returning 0
This: doc.SelectNodes("/*/Series").Count is returning 0
And this: doc.SelectNodes("/*/RootName").Count is returning 0
Making all the other xPath sequences suggested in answers not working.
EDIT:
Ok, it was the namespace, I did this:
Dim xmlnsManager As Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(doc.NameTable)
xmlnsManager.AddNamespace("ns", "http://example")
And adding ns: before every element node name in the xPath sequence. (See this for more information about it: Is it possible to specify the namespace prefix just once in a xpath expression?)
vattribute on theTimePeriodelement does not appear to be a standard xml data type value. I take it that's two date/time values (start time and end time) separated by a slash? - Steven Doggart/. I dont know if its possible with xPath, im looking for it right now. - SysDragon