xpath expression to select child node based on parent properties

11,180

Solution 1

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']"/>

Solution 2

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']/..
Share:
11,180
Laxmikanth Samudrala
Author by

Laxmikanth Samudrala

simple

Updated on June 04, 2022

Comments

  • Laxmikanth Samudrala
    Laxmikanth Samudrala about 2 years
    <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)