Question

<AppXmlLogWritter>
  <LogData>
    <LogID>235820130202134128634953894887814709273</LogID>
    <LogDateTime>20130102134128</LogDateTime>
    <LogType>Warning</LogType>
    <LogFlag>PACSFlag</LogFlag>
    <LogApplication>PACSLogApplication</LogApplication>
    <LogModule>PACSLogModule</LogModule>
    <LogLocation>PACSLogLocation</LogLocation>
    <LogText>PACSLogText</LogText>
  </LogData>
  <LogData>
    <LogID>2358201302021341286349538948878147093861</LogID>
    <LogDateTime>20130202134128</LogDateTime>
    <LogType>Error</LogType>
    <LogFlag>PACSFlag</LogFlag>
    <LogApplication>PACSLogApplication</LogApplication>
    <LogModule>PACSLogModule</LogModule>
    <LogLocation>PACSLogLocation</LogLocation>
    <LogText>PACSLogText</LogText>
  </LogData>
</AppXmlLogWritter>

XpathQuery

/AppXmlLogWritter/LogData[LogApplication/text()[starts-with(. , 'P')]and position()>='1' and position()<='3']

**Before Filter**
<Node 1> Position 1
<Node 2> Position 2
<Node 3> Position 3
<Node 4> Position 4
<Node 5> Position 5
<Node 6> Position 6   

 **After Filter**
<Node 2> Position 2
<Node 5> Position 5
<Node 6> Position 6

See in above list of nodes Before filter i have 6 nodes but when i filter i am getting three nodes so i write query for that position()>='1' and position()<='3 but result getting only 1 node In sql there is rownumber condition after filter rownumber is apply to all rows but in xml how can i achieve. I am not using LINQ to XML

Was it helpful?

Solution

Use parentheses:

(/AppXmlLogWritter/LogData[LogApplication[starts-with(. , 'P')]])[position()<=3]

If you put the position test inside the LogData predicate then it applies just to that location step and will restrict the match to those LogData elements that are the first, second or third LogData child of their respective parent elements. With the parentheses you're constructing an initial node set (of all the LogData elements whose LogApplication starts with P) and then selecting the first three nodes from that set in document order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top