2010-09-22 432 views
1

我在XSLT中有以下片段。有沒有一種方法可以在不轉義字符<>的情況下使用ASCII等效字符?XSLT轉義字符

<coord> 
... 
<xsl:with-param name="substringIn" select="'' ''"/> 
<xsl:with-param 
    name="substringOut" 
    select="'&#60;/coord&#62;&#60;coord&#62;'" 
/> 
... 
</coord> 

我有空間分隔的一串數字像

<value>1 2 3 4 5 6 7</value> 

,我想他們中的每一個轉換成XML標籤,如:

<coord>1</coord> 
<coord>2</coord> 
<coord>3</coord> 
<coord>4</coord> 
<coord>5</coord> 
<coord>6</coord> 
<coord>7</coord> 

我上面的代碼確實是,但我不想使用ASCII轉義字符。

+0

問得好(+1)。查看我的答案以獲得真正簡單的XSLT解決方案。 – 2010-09-22 16:20:57

回答

3

分割字符串和包裝中的元素的組分的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="text()" name="split"> 
    <xsl:param name="pText" select="."/> 

    <xsl:choose> 
    <xsl:when test="not(contains($pText, ' '))"> 
    <coord><xsl:value-of select="$pText"/></coord> 
    </xsl:when> 
    <xsl:otherwise> 
    <coord> 
    <xsl:value-of select="substring-before($pText, ' ')"/> 
    </coord> 

    <xsl:call-template name="split"> 
    <xsl:with-param name="pText" select="substring-after($pText, ' ')"/> 
    </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

當該轉化此XML文檔上施加:

<t>1 2 3 4 5 6 7</t> 

的想要產生正確的結果

<coord>1</coord> 
<coord>2</coord> 
<coord>3</coord> 
<coord>4</coord> 
<coord>5</coord> 
<coord>6</coord> 
<coord>7</coord> 
+0

+1好的解決方案。但我認爲規範化默認參數值會增加一個空格,只需一個'xls:if'即可構成一個緊湊的代碼。 – 2010-09-22 17:42:38

+0

@Alejandro:是的,但是在現實世界中,很少有一個空格是唯一的分隔符 - 我的解決方案在所有情況下都能正常工作,而normalize-space()解決方案僅在空格分隔符的情況下才起作用。 – 2010-09-27 12:38:23

1

您可以使用一個CDATA部分:

<xsl:with-param name="substringOut"><![CDATA[</coord><coord>]]></xsl:with-param>