Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4619072
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:22:28+00:00 2026-05-22T02:22:28+00:00

How can I preserve entity references when transforming XML with XSLT (2.0)? With all

  • 0

How can I preserve entity references when transforming XML with XSLT (2.0)? With all of the processors I’ve tried, the entity gets resolved by default. I can use xsl:character-map to handle the character entities, but what about text entities?

For example, this XML:

<!DOCTYPE doc [
<!ENTITY so "stackoverflow">
<!ENTITY question "How can I preserve the entity reference when transforming with XSLT??">
]>
<doc>
  <text>Hello &so;!</text>
  <text>&question;</text>
</doc>

transformed with the following XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

</xsl:stylesheet>

produces the following output:

<doc>
   <text>Hello stackoverflow!</text>
   <text>How can I preserve the entity reference when transforming with XSLT??</text>
</doc>

The output should look like the input (minus the doctype declaration for now):

<doc>
  <text>Hello &so;!</text>
  <text>&question;</text>
</doc>

I’m hoping that I don’t have to pre-process the input by replacing all ampersands with &amp; (like &amp;question;) and then post-process the output by replacing all &amp; with &.

Maybe this is processor specific? I’m using Saxon 9.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:22:29+00:00Added an answer on May 22, 2026 at 2:22 am

    If you know what entities will be used and how they are defined, you can do the following (quite primitive and error-prone, but still better than nothing):

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:my="my:my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:character-map name="mapEntities">
      <xsl:output-character character="&amp;" string="&amp;"/>
     </xsl:character-map>
    
     <xsl:variable name="vEntities" select=
     "'stackoverflow',
     'How can I preserve the entity reference when transforming with XSLT\?\?'
     "/>
    
     <xsl:variable name="vReplacements" select=
     "'&amp;so;', '&amp;question;'"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/">
      <xsl:text disable-output-escaping="yes"><![CDATA[<!DOCTYPE doc [ <!ENTITY so "stackoverflow">
    <!ENTITY question
    "How can I preserve the entity reference when transforming with XSLT??"> ]>
    ]]>
      </xsl:text>
    
      <xsl:apply-templates/>
     </xsl:template>
    
     <xsl:template match="text()">
      <xsl:value-of select=
      "my:multiReplace(.,
                       $vEntities,
                       $vReplacements,
                       count($vEntities)
                       )
      " disable-output-escaping="yes"/>
     </xsl:template>
    
     <xsl:function name="my:multiReplace">
      <xsl:param name="pText" as="xs:string"/>
      <xsl:param name="pEnts" as="xs:string*"/>
      <xsl:param name="pReps" as="xs:string*"/>
      <xsl:param name="pCount" as="xs:integer"/>
    
      <xsl:sequence select=
      "if($pCount > 0)
         then
          my:multiReplace(replace($pText,
                                  $pEnts[1],
                                  $pReps[1]
                                  ),
                          subsequence($pEnts,2),
                          subsequence($pReps,2),
                          $pCount -1
                          )
          else
           $pText
      "/>
     </xsl:function>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <!DOCTYPE doc [ <!ENTITY so "stackoverflow">
    <!ENTITY question
    "How can I preserve the entity reference when transforming with XSLT??"> ]>
    <doc>
        <text>Hello &so;!</text>
        <text>&question;</text>
    </doc>
    

    the wanted result is produced:

    <!DOCTYPE doc [ <!ENTITY so "stackoverflow">
    <!ENTITY question
    "How can I preserve the entity reference when transforming with XSLT??"> ]>
    
      <doc>
          <text>Hello &so;!</text>
          <text>&question;</text>
    </doc>
    

    Do note:

    1. The special (RegEx) characters in the replacements must be escaped.

    2. We needed to resolve to DOE, which isn’t recommended, because it violates the principles of the XSLT architecture and processing model — in other words this solution is a nasty hack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I create XML Schema which can preserve space in the xml ...
I want to create XML Schema which can preserve formatted text Like, Line break
How can you preserve enters given by the user in the database and show
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can I get a 'when to use' for these and others? <% %> <%#
I have an entity which is constructed from a string and can be serialized
How can I preserve the order in which the hash elements were added FOR
After merging 2 LINQ lists, how can I preserve the table objects and names?
When using 'db_table' to explicitly set the database table name, how can you preserve
I'm trying to convert all instances of the > character to its HTML entity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.