XPATH предыдущий узел - PullRequest
       1

XPATH предыдущий узел

0 голосов
/ 27 декабря 2018

У меня есть следующий 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

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...