2010-07-12 119 views
0

如何使用XSLT封装我的XML块周围的节点? 例如,我有以下XML文件。XSLT:添加节点!

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" /> 

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

我的输入XML文件看起来像这样。

<Root> 
<Location><Name>Pennsylvania</Name><Type>State</Type></Location> 
</Root> 

我希望输出看起来像这样。

 <Root><Container> 
    <Location><Name>Pennsylvania</Name><Type>State</Type></Location> 
</Container> 
    </Root> 

我想确保一个名为<CONTAINER>节点被应用于每一次,它复制了从根/位置信息。我需要对XSLT文件进行哪些更改?

+0

目前尚不清楚你想要做什么。你如何提供一个小样本XML文件和你希望得到的输出? – Welbog 2010-07-12 15:47:52

+0

我的XML文件看起来像这样。 [代码] 宾夕法尼亚 国家 [/代码] 我想这个文件转换成 [代码] 宾夕法尼亚 国家 [/代码] – abhi 2010-07-12 17:23:12

+1

那么什么是''在你的例子?如果您将其重命名为'',看起来您将拥有完全想要的内容。 – Welbog 2010-07-12 17:55:44

回答

1

总结LL在评论的答案,这样的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="Location"> 
     <Container> 
      <xsl:call-template name="identity"/> 
     </Container> 
    </xsl:template> 
</xsl:stylesheet> 

结果:

<Root> 
    <Container> 
     <Location> 
      <Name>Pennsylvania</Name> 
      <Type>State</Type> 
     </Location> 
    </Container> 
</Root> 
+0

这已部分解决了该问题。谢谢。亚历杭德罗。 – abhi 2010-07-14 16:13:01

1

我只是猜测,在猜测模式似乎你想要这个

编辑:由马德斯·汉森另一种猜测帮...

添加这到您已拥有的身份证明模板:

<xsl:template match="Location"> 
    <CONTAINER><xsl:apply-templates/></CONTAINER> 
</xsl:template> 
+0

我的XML文档中没有CONTAINER,那么它将在哪里进行匹配? – abhi 2010-07-12 17:18:24

+0

@abhi:你没有显示任何XML文档,* this *是真正的问题......当你说:“我想确保一个名为''的节点每次都得到应用” - 我明白在XML文档中有'',并且希望每次匹配时(将模板应用于该文档),则必须完成特定处理(复制“根/位置”)。 请编辑您的问题,并更好地定义它。另外,提供一个最小的XML文档! – 2010-07-12 17:52:51

+0

@Dimitre - 他想匹配'Location'与'Container'包:'的 \t \t \t的 \t ' – 2010-07-12 19:24:42