0

I am trying to remove the line from my non well - XML. But while reading the XML using the below tried XSLT, it gives me error in reading the same.

Source XML : This has two xml version declarations.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <?xml version="1.0" encoding="utf-8"?>
    <test xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
        <data>hello</data>
    </test>
</root>

Tried XSL :

<?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"/>
    <xsl:template match="/">
        <xsl:copy-of select="*" />
    </xsl:template>
</xsl:stylesheet>

Expected Output :

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <test>
        <data>hello</data>
    </test>
</root>

But i am getting error that the input XML cannot be read by XSLT. Can someone please let me know is there a way that we can adjust the xslt to read the same.

Thank you.

Ven
  • 85
  • 1
  • 9
  • 2
    XSLT requires that the XML be well-formed in order to process it. Try using some other means (script or small program) to remove the extra version line. – Doug Domeny Mar 31 '21 at 21:09
  • 1
    Your input "XML" is not well-formed, as @DougDomeny has commented. BTW, the `` is an ***XML declaration***. There may only be [at most one](https://stackoverflow.com/q/20251560/290085), and it may only be at the very top of an XML document. Prior to processing via XSLT, clean up the data using a technique such as those mentioned in [How to parse invalid (bad / not well-formed) XML?](https://stackoverflow.com/q/44765194/290085) – kjhughes Mar 31 '21 at 21:41
  • Alternatively, if you absolutely want to process it using XSLT, you could attempt the repair by processing the file as text using `unparsed-text()` and `xsl:analyze-string`. See [Parse text file with XSLT](https://stackoverflow.com/q/15974377/290085). – kjhughes Mar 31 '21 at 21:41
  • Thanks @kjhughes. I must use the XSLT only and not Java. i see the link to parse the text file with XSLT , but i am really unfamiliar with using of the same. can you be able to share me the piece of code that i can use.? Also we are using XSLT1.0 processor.thanks. – Ven Mar 31 '21 at 21:52
  • 1
    If you're stuck with XSLT 1.0, I recommend that you pre-process the file in another language. – kjhughes Mar 31 '21 at 22:52
  • Thanks, i had to use Java for the conversion and no longer using XSLT. Thank you again for your time. – Ven Apr 02 '21 at 11:03

0 Answers0