2014-09-12 29 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

相關問題