2012-04-04 96 views
1

聯合,我有以下XML:如何XML元素使用XSLT

<root> 
<section> 
    <item name="a"> 
     <uuid>1</uuid> 
    </item> 
</section> 
<section> 
    <item name="b"> 
     <uuid>2</uuid> 
    </item> 
</section> 
</root> 

我想將它轉變成以下XML:

<root> 
<section> 
    <item name="a"> 
     <uuid>1</uuid> 
    </item> 
    <item name="b"> 
     <uuid>2</uuid> 
    </item> 
</section> 
</root> 

在此先感謝。

更新。

稍微不同的示例包含其他元素和屬性。

輸入:

<root age="1"> 
<description>some text</description> 
<section> 
    <item name="a"> 
     <uuid>1</uuid> 
    </item> 
</section> 
<section> 
    <item name="b"> 
     <uuid>2</uuid> 
    </item> 
</section> 
</root> 

我想將其改造成:

<root age="1"> 
<description>some text</description> 
<section> 
    <item name="a"> 
     <uuid>1</uuid> 
    </item> 
    <item name="b"> 
     <uuid>2</uuid> 
    </item> 
</section> 
</root> 
+0

究竟什麼是問題?你有什麼嘗試?出了什麼問題? – 2012-04-04 08:13:12

+0

@Paul Butcher因爲我不熟悉XSLT,所以我沒有嘗試任何東西。問題很簡單,如何獲得理想的輸出給定輸入 – 2012-04-04 08:19:13

+0

嘗試像Elance,guru.com,vWorker或oDesk – 2012-04-04 08:25:56

回答

1

繼XSL應該工作:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output indent="yes" omit-xml-declaration="yes"/> 
    <xsl:strip-space elements="section item"/> 
    <xsl:template match="/root"> 
     <root> 
      <section> 
       <xsl:apply-templates select="section"/> 
      </section> 
     </root> 
    </xsl:template> 

    <xsl:template match="item"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 

它提供:

<root> 
    <section> 
     <item name="a"> 
     <uuid>1</uuid> 
     </item> 
     <item name="b"> 
     <uuid>2</uuid> 
     </item> 
    </section> 
</root> 

更新:

對於您可以使用以下XSL第二個例子:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output indent="yes" omit-xml-declaration="yes"/> 
    <xsl:strip-space elements="root item"/> 

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

    <xsl:template match="description"> 
     <xsl:copy-of select="."/> 
     <section> 
      <xsl:apply-templates select="following-sibling::section/item"/> 
     </section> 
    </xsl:template> 

    <xsl:template match="section" /> 

    <xsl:template match="item"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

謝謝@Vitaliy它伎倆。但它不適用於我更新的示例。您能否爲我提供更新示例的XSLT。 – 2012-04-04 08:41:29

+0

非常感謝@Vitaliy。它很棒! – 2012-04-04 09:14:24