Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath expression to select child node based on parent properties

Tags:

xml

xslt

xpath

<module>
<component>
   <section>
      <ptemplateId root="1.8"/>
      <entry>
    <observation>
       <templateId root="1.24"/>
    </observation>
      </entry>
   </section>
</component>
<component>
   <section>
      <ptemplateId root="1.10"/>
      <entry>
    <observation>
       <templateId root="1.24"/>
    </observation>
      </entry>
   </section>
</component>
<component>
   <section>
      <ptemplateId root="1.23"/>
      <entry>
    <observation>
       <templateId root="1.24"/>
    </observation>
     <entryRelation>
        <observation>
         <templateId root="1.24"/>
        </observation>
     </entryRelation>
      </entry>
   </section>
</component>
<component>
       <section>
          <ptemplateId root="1.8"/>
          <entry>
        <observation>
           <templateId root="1.24"/>
        </observation>
         <entryRelation>
            <observation>
             <templateId root="1.28"/>
            </observation>
         </entryRelation>
          </entry>
       </section>
    </component>
</module>

I would like to select observation in a template based on ptemplateId, can i know the match expression for this ?

<xsl:template match"******">
   <!-- some processing goes here to process
        observation if ptemplateId is 1.8... -->
</xsl:template>

<xsl:template match"******">
   <!-- some processing goes here to process
        observation if ptemplateId is other than   1.8... -->
</xsl:template>


 there can be nested observation's also. (i am looking for a match expression with axis expressions to make it more generic)
like image 401
Laxmikanth Samudrala Avatar asked Jul 03 '26 09:07

Laxmikanth Samudrala


2 Answers

Try this:

/module/component/section[ptemplateId/@root='1.23']//observation

Substituting the ptemplateId/@root value that you want instead of '1.23', of course. This should cover nested observations, so long as they occur anywhere as children of the section that contains that ptemplateId.

You can try this out at my online xpath tester, here.

Does this work for you?

Edit: You may also consider this variant, for placing into an <xsl:template match="..." />.

<xsl:template match="observation[ancestor::section/ptemplateId/@root = '1.23']"/>
like image 89
Chris Nielsen Avatar answered Jul 04 '26 22:07

Chris Nielsen


I can't test this right now and it's been a litle while since I did xpath but I think the following should work. It navigates down the tree to the node containing the root attribute with a value equal to 1.23 and then uses .. which refers to parrent.

//module/component/section/ptemplateId[@root='1.23']/..
like image 36
olle Avatar answered Jul 04 '26 23:07

olle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!