2013-10-10 71 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

更新了我的答案。 –