尋求使用XSLT來轉換我的XML。示例XML如下:使用XSLT重構XML節點
<root>
<info>
<firstname>Bob</firstname>
<lastname>Joe</lastname>
</info>
<notes>
<note>text1</note>
<note>text2</note>
</notes>
<othernotes>
<note>text3</note>
<note>text4</note>
</othernotes>
我在尋找提取所有「筆記」元素,並讓他們父節點下的「注意事項」。
我在尋找的結果如下:
<root>
<info>
<firstname>Bob</firstname>
<lastname>Joe</lastname>
</info>
<notes>
<note>text1</note>
<note>text2</note>
<note>text3</note>
<note>text4</note>
</notes>
</root>
我試圖用的是讓我來提取我的「筆記」的XSLT,但我想不通我怎麼能將它們重新包裝在「筆記」節點中。
下面是我使用的XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="notes|othernotes">
<xsl:apply-templates select="note"/>
</xsl:template>
<xsl:template match="*">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
我與上面的XSLT得到的結果是:
<root>
<info>
<firstname>Bob</firstname>
<lastname>Joe</lastname>
</info>
<note>text1</note>
<note>text2</note>
<note>text3</note>
<note>text4</note>
</root>
感謝
不錯,乾淨,+1。我想用來代替重新創建,這是我個人偏好的問題。另外,要創建特定命名空間中的元素,您可以輕鬆聲明一個前綴並使用它:''。 ''實際上只在元素名稱應該是動態時才需要。 –
Tomalak
2010-06-06 21:08:36