2010-07-12 118 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