這個簡短和簡單(沒有明確的條件指令)XSLT 1.0變換:
<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="/*">
<fields>
<field name="all">
<xsl:variable name="vfieldConcat">
<xsl:for-each select="field/value">
<xsl:value-of select="concat(., ' ')"/>
</xsl:for-each>
</xsl:variable>
<value><xsl:value-of select=
"normalize-space($vfieldConcat)"/></value>
</field>
</fields>
</xsl:template>
</xsl:stylesheet>
當所提供的XML文檔(良好性校正的)施加:
<fields>
<field name="first">
<value>example</value>
</field>
<field name="last">
<value>hello</value>
</field>
<field name="age">
<value>25</value>
</field>
<field name="enable">
<value>1</value>
</field>
</fields>
產生想要的,正確的結果:
<fields>
<field name="all">
<value>example hello 25 1</value>
</field>
</fields>
二, XSLT 2.0溶液
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<fields>
<field name="all">
<value><xsl:value-of select="field/value"/></value>
</field>
</fields>
</xsl:template>
</xsl:stylesheet>
當這種轉化是在同一個XML文檔(上圖)中,相同的正確的結果產生施加:
<fields>
<field name="all">
<value>example hello 25 1</value>
</field>
</fields>
說明:使用的事實,即separator
attribute of xsl:value-of
的默認值是單個空間。
應該如何處理不包含文本的'value'元素?應該導致三個空間還是隻有一個?即'例25'或'例25'? – bitmask 2012-04-18 18:29:41
如果value元素爲null,則不應該有空格。 – Ramesh 2012-04-18 18:34:01