0

I've searched for renaming an XML node to a string. I have found examples of strings within XML to Nodes but not the other way.

Is it possible to do the following

<par>
<run> Some text <break/> </run>
</par>

<par>
<run> some text with no carraige return</run>
</par>

To:

<par>
<run> Some text &#10; </run>
</par>

Many thanks in advance for any replies.

Dono

1 Answer 1

1

Certainly that's possible. Just use an identity transform and handle <break> specially. Instead of copying it using <xsl:copy>, output any text you like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="break">
    <xsl:value-of select="'&#10;'"/>
  </xsl:template>

</xsl:stylesheet>

I you want the literal output &#10; you can use disable-output-escaping, like

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="break">
    <xsl:value-of select="'&amp;#10;'" disable-output-escaping="yes"/>
  </xsl:template>

</xsl:stylesheet>

However, this is an optional feature and not guaranteed to be supported by any XSLT processor.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a legend my friend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.