XPATH前一个节点

时间:2018-12-27 11:26:04

标签: c# xpath

我有以下XML:

XML

 <PatientDetailsXML>             
 <PList> 
           <PName type="Patient">    // node [i] = 0 
        <properties>
            <Room bedType="Auto" />
            <PName title="Joe Beom" PId="1234">
                <Details>
                    <classification classification="paymenttype" category="Wallet" />
                    <classification classification="Humor" category="1" />
                    <classification classification="Food" category="Fruit" />
                </Details>
            </PName>
            </properties>
        <childEvents>
        </childEvents>
    </PName>
            <PName type="Patient"> // node[i] =1
        <properties>
            <Room bedType="Auto" />
            <PName title="John Bair" PId="4567">
                <Details>
                    <classification classification="paymenttype" category="Wallet" />
                    <classification classification="Humor" category="2" />
                    <classification classification="Food" category="Fruit" />
                </Details>
            </PName>
            </properties>
        <childEvents>
        </childEvents>
    </PName>

            <PName type="Patient"> // node[i] = 2
        <properties>
            <Room bedType="Auto" />
            <PName title="Bairstow" PId="1234">
                <Details>
                    <classification classification="paymenttype" category="CreditCard" />
                    <classification classification="Humor" category="1" />
                    <classification classification="Food" category="Vegetables" />
                </Details>
            </PName>
            </properties>
        <childEvents>
        </childEvents>
    </PName>

当我在node[0]时,我正在搜索classification='paymenttype'category ='CreditCard'时,我发现第三个项目节点[i] = 2

  var next = currNode.SelectSingleNode(@"following::classifications/classification[@classification='paymenttype'and @category ='CreditCard'] /ancestor::PName/@title");  // This gives me the title `Bairstow`

现在,我需要跳到classification='paymenttype'and @category ='CreditCard'的上一个节点。

var previous = next.SelectSingleNode(@"preceding::classifications/classification[@classification='paymenttype'and @category ='CreditCard']/ancestor::event"); 
// this doesn't seem to work

代码

var query = @"//PList/PName[.//PName/classifications/classification[@classification='paymenttype' and @category='Wallet']]";
var nodes = docs.SelectNodes(query).OfType<XmlNode>().ToArray();
XmlNodeList nodeList = docs.SelectNodes(query);


 for (int i = 0; i < nodes.Count(); i++)
{

  //do something
  //jump to method to do something else 

   string testPayment = Externals.Next(nodes[i]);

}

public static string Next(XMLNode currNode)
{
  var next = currNode.SelectSingleNode(@"following::classifications/classification[@classification='Humor'and @category ='1'] /ancestor::PName/@title");  
 // This gives me the title `Bairstow`

// Now I need to select the title of the previous node e.g.John Bair

var previous = next.SelectSingleNode(@"preceding::classifications/classification[@classification='paymenttype'and @category ='CreditCard']/ancestor::event");  
// this is not pointing to the node with title JohnBair

}

0 个答案:

没有答案