开发者

XSLT find and replace carriage returns

开发者 https://www.devze.com 2023-04-01 20:50 出处:网络
I need to replace carriage returns from input XML. Input as follows: <Answer> <Label>Notes/Comments</Label>

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
     开发者_如何学Go                       </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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消