tags:

views:

1277

answers:

3
<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)
+1  A: 

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']/..
olle
The parent of ptemplateId is going to be the section, and the question is asking for the observations... Perhaps this would work, though? //module/component/section/ptemplateId[@root='1.23']/..//observation
Chris Nielsen
+3  A: 

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']"/>
Chris Nielsen
A: 

An alternative would be the use of 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 yields:

<observation>
  <templateId root="1.24" />
</observation>
<observation>
  <templateId root="1.24" />
</observation>
<observation>
  <templateId root="1.28" />
</observation>
Tomalak