2013-10-10 69 views
-3

我是XSLT轉換的新手。
這是我以前的question的延伸。現在我正在複製像這樣的所有節點,然後根據前面的question的答案轉換它們。XSLT模板:只需複製子元素

<!-- Copy everything --> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

但是輸入有兩個父元素。

<Parent1> 
    <Parent11> 
    <Element1> 
    <!--Rest of the xml I want to work with--> 
    </Element1> 
    <Parent11> 
    <Parent21> 
    <Other></Other> 
    </Parent21> 
<Parent1> 

我嘗試使用

<xsl:template match="Element1"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

但隨後回到我以純文本格式的XML值。值爲<Parent21>孩子。
基於答案我想

<xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="Parent1/Parent11/Element1"/> 
     </xsl:copy> 
     </xsl:template> 

該返回我什麼。

回答

0

如果你只想要處理的部件1然後保持平時的身份模板

<!-- Copy everything --> 
<xsl:template match="@* | node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
</xsl:template> 

並添加/另一個模板:

<xsl:template match="/"> 
    <xsl:apply-templates select="Parent1/Parent11/Element1"/> 
</xsl:template> 

這將「跳過」直部件1,並繼續正常從那裏處理,忽略文檔的其餘部分。然後,您可以使用其他模板匹配Element1內的節點來執行您需要的任何其他處理。

如果文檔中有多個Element1,則需要在它們周圍包裝一個根元素以使輸出格式良好,例如,

<xsl:template match="/"> 
    <root> 
    <xsl:apply-templates select="Parent1/Parent11/Element1"/> 
    </root> 
</xsl:template> 
+0

我會在哪裏使用它?在轉換結束時,我將擁有多個'Element1'。 –

+0

更新了我的答案。 –