简体   繁体   中英

XSLT 1.0 Output XML escaped characters using numeric references

I'm using libxslt (xsltproc command) to transform an XML document into another XML.

I wonder if there is a simple way to request libxslt to use numeric references for XML escaped characters in the output (such as & for & ). The reason is that the application consuming the output does not understand the predefined entities ( & ), but does understand the numeric character references.

I understand there are some complicated ways to achieve this, but I'm thinking maybe I'm missing a simple stylesheet indication or command line option that would do just that. If not, it would probably make more sense to modify the consuming application.

To give an example, the input XML is:

<?xml version="1.0" encoding="UTF-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <B>&lt; &gt; &quot; &amp; &apos;</B>
  <B>&#60; &#62; &#34; &#38; &#39;</B>
</A>

The XSL is:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <xsl:apply-templates select="/A/B" />
  </xsl:template>

  <xsl:template match="B">
    <X><xsl:attribute name="ATTR"><xsl:value-of select="." /></xsl:attribute></X>
    <xsl:text>&#x0A;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

The result is:

<?xml version="1.0" encoding="UTF-8"?>
<X ATTR="&lt; &gt; &quot; &amp; '"/>
<X ATTR="&lt; &gt; &quot; &amp; '"/>

The result I'm after would be:

<?xml version="1.0" encoding="UTF-8"?>
<X ATTR="&#60; &#62; &#34; &#38; '"/>
<X ATTR="&#60; &#62; &#34; &#38; '"/>

I tried adding the following before the stylesheet:

<!DOCTYPE xsl:stylesheet [
  <!ENTITY amp "&#38;">
]>

or

  <!ENTITY amp "&#38;#38;">

But it didn't work (ie the result is the same with or without the entity definition).

I also understand that character-map s are not an option because they're not available in XSLT 1.0.

Standards like XML bring enormous cost savings if everyone adheres to them, and you get none of the benefits if you try to use modified or restricted or extended versions of the standard. So if you possibly can, you should change your receiving application to accept any legal XML, rather than changing your sending application to generate a restricted subset.

If the receiving application doesn't accept &amp; that presumably means it's using some kind of home-brew parser rather than a standard XML parser, and that probably means it's got lots of other unnecessary limitations as well, which will gradually emerge over the lifetime of the application.

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