XSLT find and replace carriage returns

半城伤御伤魂 提交于 2019-12-10 22:27:44

问题


I need to replace carriage returns from input XML.

Input as follows:

            <Answer>
            <Label>Notes/Comments</Label>
            <Value>Q
                            WERTYU IOPASDFGHJKLZXCVBNM 
                            QWERTYUIOPASDF GHJKLZXCVBNM
                            QWERTYU IOPASDFGHJKLZXCVBNM
                            QWERTYUIOPASDFGHJ KLZXCVBNM
                            QWERTYU IOPASDFGHJKLZXCVBNM
                            QWERTYUIOPASDFGH JKLZXCVBNM
                            QWERTYUIOPASDF GHJKLZXCVBNM
                            QWERTYUIOP ASDFGHJKLZXCVBNM
                            QWERTYU IOPASDFGHJ KLZXCVBNM
                            QWERTY UIOPAS DFGHJKLZX CVBNM
                            QWERTYUIO PASDFGHJ KLZXC VBNM
                            </Value>
            <Iteration>0</Iteration>
            <DataType>TEXT</DataType>
        </Answer>

I'm attempting to remove carriage returns using the following function:

    <xsl:template name="string-replace-all">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
        <xsl:when test="contains($text,$replace)">
            <xsl:value-of select="substring-before($text,$replace)"/>
            <xsl:value-of select="$by"/>
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                <xsl:with-param name="replace" select="$replace"/>
                <xsl:with-param name="by" select="$by"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Calling it as follows:

        <xsl:when test="Label='Notes/Comments'">              
                        <xsl:element name="Comments">
                            <xsl:call-template name="string-replace-all">
                                <xsl:with-param name="text" select="Value" />
                                <xsl:with-param name="replace">&#xA;</xsl:with-param>
                                <xsl:with-param name="by" select="'. '" />
                            </xsl:call-template>                
          </xsl:element>
        </xsl:when>

But so far to no success. I'm hoping it's just that the character (&#xA;) I'm passing in is incorrect but I can't get it working.

UPDATE

As it turns out in this case, removing more white-space characters than just Carriage returns was acceptable and so normalize-space() fulfils my requirements.


回答1:


Have you checked that you don't have a &#xD; there besides the &#xA; ?



来源:https://stackoverflow.com/questions/7286579/xslt-find-and-replace-carriage-returns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!