How to remove extra xml declaration from xml in XSLT?

1.5k Views Asked by At

I am transforming a idml file into a xml in my project.I have an issue on extra xml declaration. Here is the part of idml file :`

<Content> &lt;?xml version="1.0" encoding="UTF-16"
    standalone="yes"?&gt;&#x2028;&lt;math&gt;
    &lt;/math&gt;</Content>

` This is the output I am getting after transformation:

    <?xml version="1.0" encoding="UTF-8"?>
<content>
    <?xml version="1.0" encoding="UTF-16" standalone="yes"?>
    <math></math>
</content>

My task is to replace &lt; and &gt; with < and >,then I got this issue..

I tried lots of ways to do this(not standard things)..Finally I tried to replace word xml replace with space and convert this to PI and transform PI in post processing mode..but it also was not succeeded

<xsl:template match="Content">
    <xsl:if test=".[contains(text(),'math')]">
         <xsl:value-of select="replace(./text(),'xml','')”/>
    </xsl:if>
</xsl:template>

Output I want to generate:

<?xml version="1.0" encoding="UTF-8"?> <content><math></math></content>

And I am using Saxon 9.7.0HE for my development.

Please help me to solve this issue..Thank you..!

2

There are 2 best solutions below

0
On

I know it's an old question, but people who stumble upon it might still like to see an answer that applies to a version="1.0" stylesheet. So here's a solution that works by simply removing the first line / XML declaration:

  <xsl:template match="Content">
    <xsl:value-of disable-output-escaping="yes" select="replace(., '^\s*&lt;\?xml.*?\?&gt;', '')"/>
  </xsl:template>
2
On

In a version="3.0" stylesheet, you could use

<xsl:template match="Content">
    <content>
        <xsl:copy-of select="parse-xml(normalize-space())"/>
    </content>
</xsl:template>

however with your input snippet, that is not working, as the contents is not well-formed, given the &#x2028; character before the root element of the escaped markup. So it is not quite clear what kind of content you have there, if it is not an XML document nor an XML fragment then neither parse-xml nor parse-xml-fragment will help.