Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT and Carriage Returns

Status
Not open for further replies.

MrXSLT

Programmer
Mar 10, 2004
1
US
I'm needing to use the XSLT "substring-before" and "substring-after" functions to parse an XPATH value that is delimted by Carriage Returns. For a simple example, assuming I have a value in a XML document with a "comments" element that comes in as:
<comments>
Hello
World
</comments>
In this case, the words "Hello" and "World" are separated by a carriage return. I need to transform the <comments> element into <comments1> and <comments2> in my target XML doc. I need the word "Hello" to display in <comments1> and "World" to display in <comments2>

Assume I assign the value from the <comments> element to a variable named "var1":
<xsl:variable name='var1' select="//comments"/>

In order to get the value "Hello" that comes before the carriage return, I've tried using:
select="substring-before($var1, '#x0D')"

But that does not work. I've also tried it with the delimeter "#xD" and "\r", but they don't work either. Does anyone know how to do this?
 
From the XML1 specification section 2.11:

"XML parsed entities are often stored in computer files which, for editing convenience, are organized into lines. These lines are typically separated by some combination of the characters carriage-return (#xD) and line-feed (#xA).

To simplify the tasks of applications, the characters passed to an application by the XML processor must be as if the XML processor normalized all line breaks in external parsed entities (including the document entity) on input, before parsing, by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character."

Here is a simple demo stylesheet of how to split text at line breaks based on the original example:

Code:
<?xml version="1.0"?>
<demo>
<comments>Hello
World</comments>
</demo>

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" 
     xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="html"/>

<xsl:variable name="UC10" select="'&#10;'"/>

<xsl:template match="/">
     <xsl:apply-templates select="demo/comments"/>
</xsl:template>

<xsl:template match="comments">
      <xsl:variable name="TMP" select="." />
      <xsl:if test="contains($TMP, $UC10)">
         comment1: <xsl:value-of select="substring-before($TMP, $UC10)"/>
         comment2: <xsl:value-of select="substring-after($TMP, $UC10)"/>
      </xsl:if>
</xsl:template>

</xsl:stylesheet>

Enjoy,
-Finnbarr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top