Rahul
Rahul

Reputation: 21

XML tags missing or skipped during xsl transformation

We are performing an xsl transformation to convert hexcode entity to mdash. We also have converted all & in xml to output as &. This transformation happens as expected, but along with that some entities are escaped like ' converting into 's and similar is the case of some more entities like § and few more entities.

And also, all tags are skipped from the xml only content is rendered in output and tags are skipped.

Ex->

Input before xsl transformation:-

<?xml version="1.0" encoding="UTF-8"?><name>abc<name>

Output after xsl transformation:-

abc

Below is the xsl used:-

<xsl:stylesheet version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  
<xsl:output method="xml" omit-xml-declaration="no" use-character-maps="mdash amp" />

<xsl:character-map name="mdash">
  <xsl:output-character character="&#x2014;" string="&amp;mdash;" />
</xsl:character-map>

<xsl:character-map name="amp">
  <xsl:output-character character="&amp;" string="&amp;" />
</xsl:character-map>

<xsl:mode on-no-match="shallow-copy"/>

</xsl:stylesheet>

This is how transformation happens:-

TransformerFactory tfactory = new net.sf.saxon.TransformerFactoryImpl();
InputStream xsl_stream = new BufferedInputStream(new FileInputStream(xsl_path));
StreamSource xsl_source = new StreamSource(xsl_stream, xsl_path);
Transformer transformer = tfactory.newTransformer(xsl_source);
InputStream xml_inputsource = new BufferedInputStream(new FileInputStream(output_file));
StreamSource xmlSource = new StreamSource(xml_inputsource, output_file);
StreamResult xmlResult = new StreamResult(finalFile);
transformer.transform(xmlSource, xmlResult); 

Is there a way to preserve xml tags in output and also unescape entites i.e. &apos; should remain &apos; after xml transformation. All entities should remain as it is after transformation.

Upvotes: 1

Views: 114

Answers (2)

Michael Kay
Michael Kay

Reputation: 163587

All entities should remain as it is after transformation

That can't be achieved because entity and character references are expanded by the XML parser before the transformation even starts.

The best you can do is to use xsl:character-map to convert the characters that you want to appear as entity references:

<xsl:character-map name="specials">
  <xsl:output-character character="&#x2014;" string="&amp;mdash;" />
  <xsl:output-character character="&#x00A7;" string="&amp;sect;" />
  .. etc ..
</xsl:character-map>

I don't know what you are trying to achieve by outputting & as &amp;. That seems misguided. But to be honest, I'm not really sure what you are trying to achieve by any of this. Telling us what output you want is one thing, but the real question is Why?.

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 117102

Your question is tagged as both saxon and xalan, which makes no sense whatsoever.

If you're using Xalan, which supports only XSLT 1.0, to process an XSLT 3.0 stylesheet that contains no templates, the result will be the string-value of the root element - see: https://www.w3.org/TR/1999/REC-xslt-19991116/#built-in-rule

Upvotes: 2

Related Questions