SoftwareSavant
SoftwareSavant

Reputation: 9767

Need to figure out xpath to get sibling node

I have to return a certain node that is a sibling to a node that I am using to select a certain parent of both nodes...

suppose...

<?xml version="1.0" encoding="utf-16"?>
<dataTemplateSpecification id="id1" name="name1">
<templates xmlns="">
<template>
  <elements>
    <element id="element0" name="PatientId" display="Patient ID" dataType="String" visable="true" readOnly="false" value="32">
      <mapping path="//Template/TemplateData/ACOData/PATIENT_ID" />
      <validation>
        <rules>
          <rule id="r0" test="#element0.value == ''">
            <fail>
              <html>
                <b>Patient ID is null, value must be present</b>
              </html>
            </fail>
          </rule>
        </rules>
      </validation>
    </element>
    <element id="element4" name="Active" display="ACTIVE" dataType="String" visable="true" readOnly="true" value="A">
      <mapping path="//Template/TemplateData/ACOData/ACTIVE" />
      <!--//Templates/Patient/sources/source/empi"/>-->
      <validation>
        <rules>
          <rule id="r1" test="#element1.value == ''">
            <fail>
              <html>
                <b>EMPI ID is null, value must be present</b>
              </html>
            </fail>
          </rule>
        </rules>
      </validation>
    </element>
    <element id="element2" name="PopulationPatientID" display="Population Patient ID" dataType="String" visable="true" readOnly="true" enc="223" value="198">
      <mapping path="//Template/TemplateData/ACOData/POPULATION_PATIENT_ID" />
      <!--Patient/compositeID[./idType='populationPatientID']/id-->
      <validation>
        <rules>
          <rule id="r1" test="#element1.value == ''">
            <fail>
              <html>
                <b>EMPI ID is null, value must be present</b>
              </html>
            </fail>
          </rule>
        </rules>
      </validation>
    </element>
    <element id="element1" name="EncounterId" display="Encounter ID" dataType="String" visable="true" readOnly="false" value="223">
      <mapping path="//Template/TemplateData/ACOData/FOCUSED_READMISSIONS_ID" />
      <validation>
        <rules>
          <rule id="r0" test="#element0.value == ''">
            <fail>
              <html>
                <b>Patient ID is null, value must be present</b>
              </html>
            </fail>
          </rule>
        </rules>
      </validation>
    </element>

The Xpath I have right now currently only gets the right template. But I need the right element...

//dataTemplateSpecification/templates/template[./elements/element[@name="PopulationPatientID" and @value="198" and @enc="223"]]

I need to xpath to the node that has an attribute named "Active" Is that even possible? I was thinking I might need to drill backwords in the [] section... you know [./../../] where I would be selecting by a finer granularity before then... //dataTemplateSpecification/templates/template/elements/element[./../../] ect.. Does that make sense or am I completely rambling here? Any help would be appreciated. Thanks.

Upvotes: 1

Views: 839

Answers (3)

SoftwareSavant
SoftwareSavant

Reputation: 9767

@royerboat and @Dimitre Novatchev...

both of you guys had the right idea and it inspired me to get the Xpath that I needed...

//dataTemplateSpecification/templates/template[./elements/element[@name="PopulationPatientID" and @value="198" and @enc="223"]]//element[@name = 'Active']

That xpath is precisely what I need. Dimitre, I tried both of your sugestions and they came close, but no cigar. Thanks for the help guys.

Upvotes: 0

cookie_monster
cookie_monster

Reputation: 39

The reason you are getting the template instead of the element with your XPath search is because you are searching for template.

//dataTemplateSpecification/templates/template[./elements/element[@name="PopulationPatientID" and @value="198" and @enc="223"]]

If you want the element instead, you need to specify it before the predicate part of the XPath statement (the predicate being the part in [ ] or brackets).

Also, if you are looking for the element with the name attribute that has a value of "Active", you can specify it as part of your XPath statement.

Either one of the following statements will get the element with that has a name of "Active":

/dataTemplateSpecification/templates/template/elements/element[@name = 'Active']

//element[@name = 'Active']

Upvotes: 2

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

Use:

/*/*/*/*/element
          [@name="PopulationPatientID" and @value="198" and @enc="223"]
            /preceding-sibling::element[1]

or even simpler:

/*/*/*/*/element[@name='Active']

Upvotes: 1

Related Questions