2

I need to create a XSLT code for this but after reaching 'Response' code fails. And since I'm receiving it from remote service I can not change it. Please give some suggestions for the same. XML that I'm receiving in response:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<Response><?xml version="1.0" encoding="UTF-8"?>
<ResponseCode>
0
</ResponseCode>
</Response>
</soapenv:Body>
</soapenv:Envelope>
alseether
  • 1,889
  • 2
  • 24
  • 39
user2971907
  • 21
  • 2
  • 6
  • 1
    XML document validity is binary. Either it is valid XML or it is not XML. You can only have a single XML declaration at the start of the file. So your example is *not XML*. Some parsers might not reject it, but that would be an extension to process some cases of not-XML by that parser. You need to contact the remote service and get them to fix their response. – Richard Nov 24 '17 at 11:21
  • 1
    Are you sure the `Response` element contains that other document unescaped as shown? Usually the inner document is escaped in a CDATA section e.g. `<![CDATA[0]]>`. In that case assuming XSLT 3 you can parse the inner document using `parse-xml`. – Martin Honnen Nov 24 '17 at 11:40

1 Answers1

4

If you have two XML declarations in a file then your file is not XML, and if it is not XML then it cannot be processed using XSLT.

If you want to process non-XML files then you will need to use some non-XML technology to convert them to XML before you start. (However, it's best to try and persuade the supplier to fix their bug if you can. The whole point about standards is that they save costs for everyone provided you actually follow the rules.) If you do need to repair broken XML files, you can sometimes use a utility such as HTML Tidy or validator.nu, or you may need a custom Perl script or similar.

LATER

Actually, I think I should qualify this answer. If you know that your file contains a sequence of well-formed XML documents, each starting with an XML declaration, then it might be feasible to read it in an XSLT 3.0 stylesheet using the unparsed-text() function, split it into its constituent documents using xsl:analyze-string, parse each of those documents using the XPath 3.0 parse-xml() function, and then process the documents as normal. You would have to make sure you knew the file encoding in advance, and you would have to be confident there are no "nasties" in there, like something masquerading as an XML declaration inside a comment or CDATA section.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164