下面是一個簡單的轉換 - 一開始:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pIndents" select="' '"/>
<xsl:template match="*[*]">
<xsl:param name="pcurrentIndents" select="''"/>
<xsl:value-of select="concat($pcurrentIndents, name(), ':')"/>
<xsl:value-of select="concat('
',$pcurrentIndents, '{')"/>
<xsl:apply-templates>
<xsl:with-param name="pcurrentIndents" select=
"concat($pcurrentIndents, $pIndents)"/>
</xsl:apply-templates>
<xsl:value-of select="concat('
',$pcurrentIndents, '}')"/>
</xsl:template>
<xsl:template match="*[not(*)]">
<xsl:param name="pcurrentIndents" select="''"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="concat($pcurrentIndents, name(), ':')"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
當這種轉變是在所提供的XML文檔應用:
<person>
<name>
<name-first>foo</name-first>
<name-last>bar</name-last>
</name>
<age>20</age>
<city>nowhere</city>
</person>
想要的,正確的結果產生:
person:
{ name:
{
name-first:foo
name-last:bar
}
age:20
city:nowhere
}
說明:
有匹配的元素有兩個模板。
模板匹配*[*]
匹配具有子元素的元素。它使用name()
函數,:
字符,NL字符,當前縮進(空格數)和{
字符生成當前匹配元素的名稱。然後,將模板應用於當前匹配元素的子節點,參數$pcurrentIndents
傳遞當前縮進,增加空間字符的預增量(如全局參數$pIndents
中指定的那樣)。最後,在新行上使用當前縮進,最後的花括號是putput。
匹配*[not(*)]
(沒有任何元素作爲子元素的元素)的模板類似但更簡單。它輸出當前縮進處匹配元素的名稱和:
字符。在這裏應用模板爲非元素節點調用XSLT內置模板 - 在這種情況下,將選擇與文本節點匹配的內置模板,並將文本節點複製到輸出。
許多編程語言都有一個XML庫,因此您可以輕鬆創建一個快速程序來爲您執行此操作。如果您最喜歡的語言不包含XML庫,那麼您也可以使用字符串操作來完成此操作。 – 2011-05-23 10:48:25
@ingo vals - 謝謝你的迴應,@代碼 - 我已經試過了下面的代碼:<?xml version =「1.0」encoding =「ISO-8859-1」?> \t \t <的xsl:for-每個選擇= 「*」> \t { \t \t的 :\t的 \t} \t的xsl:for-each>的 \t XSL:模板> XSL:樣式表>,但我沒有得到我想要的東西。請幫忙。 –
novein
2011-05-23 10:55:56
好問題,+1。查看我的答案,獲取完整,簡短的XSLT解決方案。 – 2011-05-23 13:19:00