0

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..!

user2490093
  • 69
  • 10
  • Which XSLT 2.0 processor exactly do you use? What is the output you want to create for the input you have posted? – Martin Honnen Feb 03 '17 at 14:04
  • @ Martin I want to generate output as follows: and I am using saxon 9 in my development.. – user2490093 Feb 03 '17 at 14:14
  • Please edit the question and show the output you want there as a code snippet. As for Saxon 9, which version exactly? Saxon 9.7 has support for `parse-xml` for instance, earlier commercial releases have support for a similar extension function, that way you could parse the content of the `Content` element as XML and output the nodes and you would get rid of the nested, escaped XML declaration in a clean way. – Martin Honnen Feb 03 '17 at 14:19
  • @Martin I apologise..edited the question..and I have no clear idea on parse-xml..searching for some documentation.. – user2490093 Feb 03 '17 at 14:32

2 Answers2

1

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Did you mean xslt 3.0 as version here? I am using xslt 2.0 ..
 are handled in the code but I removed as more nodes complicate. If I am correct input is idml and output I am getting is xml (both are xml formats).. – user2490093 Feb 03 '17 at 15:02
  • 1
    The only way with Saxon 9.7 HE to parse XML inside of element data using `parse-xml` is to set `version="3.0"` so you would need to change the version if you want to use that approach. – Martin Honnen Feb 03 '17 at 17:06
0

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>