2009-09-10 157 views
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> 

我想在基于ptemplateId的模板中选择观察,我可以知道匹配表达式吗?xpath表达式根据父属性选择子节点

<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) 

回答

6

试试这个:

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

代你想要的,而不是 '1.23',当然ptemplateId/@根值。这应该涵盖嵌套观察,只要它们出现在包含该ptemplateId的部分的子节点的任何地方。

你可以在我的在线xpath测试仪here上试试。

这是否适合您?

编辑:你也可以考虑这个变种,用于放入<xsl:template match="..." />

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

我现在不能测试这个,因为我做了xpath,但我认为下面应该可以工作。它将树导航到包含根值属性的节点,其值等于1.23,然后使用..表示parrent。

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

ptemplateId的父将是一节,并且问题是要求观察......虽然这可能会起作用吗? //module/component/section/ptemplateId[@root='1.23']/..//observation – 2009-09-10 23:53:56

0

另一种方法是使用一个XSL关键的:

<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> 

上面收率:

<observation> 
    <templateId root="1.24" /> 
</observation> 
<observation> 
    <templateId root="1.24" /> 
</observation> 
<observation> 
    <templateId root="1.28" /> 
</observation> 
相关问题