...">

Xpath for selecting a child of a node based on parent properties

<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 an observation in a template based on ptemplateId, can I find out a matching 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 also. (i am looking for a match expression with axis expressions to make it more generic)
+3
source share
3 answers

Try the following:

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

Substituting the ptemplateId / @ root value you want instead of “1.23”, of course. This should cover nested cases if they occur anywhere in the form of children of the section containing this ptemplateId.

You can try this on my online xpath test, here .

Does this work for you?

Edit: You can also consider this option for posting in <xsl:template match="..." />.

<xsl:template match="observation[ancestor::section/ptemplateId/@root = '1.23']"/>
+6

, litle, xpath, , . node, , 1,23, .. .

//module/component/section/ptemplateId[@root='1.23']/..
+2

An alternative would be to use an XSL key:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <!-- the key indexes all <observation> elements by their ptemplateId -->
  <xsl:key 
    name="kObservation" 
    match="observation" 
    use="ancestor::section[1]/ptemplateId/@root" 
  />

  <xsl:template match="/">
    <!-- you can then select all the matching elements directly -->
    <xsl:apply-templates select="key('kObservation', '1.8')" />
  </xsl:template>

  <xsl:template match="observation">
    <!-- (whatever) -->
    <xsl:copy-of select="." />
  </xsl:template>

</xsl:stylesheet>

The above gives:

<observation>
  <templateId root="1.24" />
</observation>
<observation>
  <templateId root="1.24" />
</observation>
<observation>
  <templateId root="1.28" />
</observation>
0
source

Source: https://habr.com/ru/post/1717388/


All Articles