2014-09-12 57 views
0

输入XML:如何检查根元素在xml中使用xslt有一个特定的子元素?

<body> 
    <ce:sections> 
     <ce:section> 
      <ce:label>1</ce:label> 
      <ce:section-title>Introduction and main results</ce:section-title> 
      <ce:para> The existence of a globally hyperbolic Lorentzian metric on a 
       <mml:math>(3 + 1)</mml:math> 
       -spacetime with closed Cauchy surface excludes all but one differentiable structure on the underlying manifold, as observed by ChernovNemirovski 
       <citegroup>[ 
        <cite> 
         <no>CN13</no> 
         <id>CN</id> 
        </cite> 
       ]</citegroup> 
      </ce:para> 
     </ce:section> 
    </ce:sections> 
</body> 

如何检查<mml:math> .... </mml:math><citegroup> ... </citegroup>使用body/ce:section/ce:sections路径的输入XML子元素的XSLT?

如何使用xslt获得<mml: math>的路径?

+0

在本例中,它们不是'ce:section'的_child_元素。它们是'section'元素的_descendants_,而'para'的_children_。 – 2014-09-12 12:12:50

+0

我同意它不是一个孩子的元素。它是ce:section的后裔。我想知道如何获得mml:math的路径,以及如何检查它是否是ce:section的后代? – Anitha 2014-09-12 12:16:49

回答

0

要检查一个节点是否从另一个节点下降,请使用ancestor。例如:

<xsl:template match="mml:math"> 
    <xsl:if test="ancestor::ce:section"> 
    <xsl:text>This math node IS inside a section</xsl:text> 
    </xsl:if> 
</xsl:template> 

如何确定节点的路径已通过here询问。您可能会发现有助于讨论的想法。

+0

你不需要测试。只要做'match ='mml:match [ancestor :: cd:section]“'。你的方法会“吃”所有*'mml:math'元素,而不仅仅是他想要的元素。 – 2014-09-12 13:37:45

+0

我明白这个问题是“给出一个'mml:math'我怎么知道它是从'ce:section'下降的。”。这个例子演示了'祖先'如何回答这个问题。这个模板本身是无用的。提问者可以适应这个想法,但是可以参考任何'mml:math'获得 – biscuit314 2014-09-12 13:56:01

+1

只是猜测,但也许OP就像很多程序人员来到XSLT,当他们的意思是“写仅适用于“的规则。如果你真的需要“检查”某些东西,当然可以使用'xsl:if',但根据我的经验,在大多数情况下,这是一种“代码味道”,比如'xsl:for-each',尝试以命令式的方式编写XSLT代码。 – 2014-09-12 16:45:03

相关问题