简体   繁体   中英

Reading breaks in XML file when using CDATA and XSL

I am trying to read a line break when using CDATA but with little luck. Is there anything I am doing wrong? I keep getting:

This Picture was created by <br /> Type-Style back in 2007. as the output, the line break is not recognised at allm

XML

<s xmlns:b="http://crownpublishing.com/imprint/crown-business/">
    <b:bio>
        <b:about>
        <![CDATA[This Picture was created by <br /> Type-Style back in 2007. ]]>
        </b:about>
    </b:bio>
</s>

XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://crownpublishing.com/imprint/crown-business/" version="1.0">
<xsl:output cdata-section-elements="about"/>
<xsl:template match="/">
    <xsl:value-of select="s/b:bio/b:about"/>
</xsl:template>
</xsl:stylesheet>

This answer assumes that you cannot change the input document. Use string replacement to transform the br string into an actual line break. With XSLT 1.0, the standard way of doing string replacement is by writing a recursive template .

XSLT Stylesheet

The named template is shamelessly stolen from Mads' answer here which in turn borrows from Dave Pawson .

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:b="http://crownpublishing.com/imprint/crown-business/" version="1.0"
    exclude-result-prefixes="b">

    <xsl:output method="xml"  indent="yes"/>

    <xsl:template match="/">
    <result>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="/s/b:bio/b:about"/>
          <xsl:with-param name="replace" select="'&lt;br /&gt;'" />
          <xsl:with-param name="with" select="'&#10;'"/>
        </xsl:call-template>
    </result>
    </xsl:template>

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

</xsl:stylesheet>

XML Output

<?xml version="1.0" encoding="utf-8"?>
<result>
        This Picture was created by 
 Type-Style back in 2007. 
        </result>

Try this solution online here .

A literal newline in your source document will carry through to the output. Try:

<s xmlns:b="http://crownpublishing.com/imprint/crown-business/">
    <b:bio>
        <b:about>
        <![CDATA[This Picture was created by
Type-Style back in 2007. ]]>
        </b:about>
    </b:bio>
</s>

<br /> doesn't have any special meaning in XML. On the other hand, if you intend to be producing XHTML as output then what you've got only makes sense in a browser, and there it should appear as a newline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM