An And
An And

Reputation: 155

How to use xslt on an xml that does not have all the required schem details in the namespace

I have an XML (coming out of a web service call - provided below) which is an invalid XML document as it does not contain required declaration of the xsi namespace-prefix for the schema namespace.

It does not declare schema namespace - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" but uses the same within the xml.

Example nodes are - <messages xsi:nil="true" />, <value4 xsi:nil="true" /> etc.

Because of this I am not able to use XSLT transformation on the XML and my app fails with an error

The prefix "xsi" for attribute "xsi:nil" associated with an element type "messages" is not bound.".

All my test XSLTs also gave the same error. Would there be a way to apply an/any XSLT to remove all the elements that are referring to xsi? Removing them will make it a valid XML fit for consumption by others.

The XML returned from the webservice:

<?xml version="1.0" encoding="utf-8"?>
<p849:retrieveAllValues xmlns:p849="http://package.de.bc.a">
    <retrieveAllValues>
        <messages xsi:nil="true" />
        <existingValues>
            <Values>
                <value1> 10.00</value1>
                <value2>123456</value2>
                <value3>1234</value3>
                <value4 xsi:nil="true" />
                <value5 />
            </Values>
        </existingValues>
        <otherValues xsi:nil="true" />
        <recValues xsi:nil="true" />
    </retrieveAllValues>
</p849:retrieveAllValues>

Upvotes: 1

Views: 924

Answers (2)

Michael Kay
Michael Kay

Reputation: 163458

When you get ill-formed or invalid XML arriving in your system, (which I usually call non-XML because really it isn't XML at all), you should attempt to trace it to its source, and fix the problem as close to the source as you can. If you can't eliminate the problem at source, you need to repair the non-XML as early as you can. The repair technique depends on how systematic the faults are, and on the nature of the faults you need to repair. In this case, your data is technically XML-well-formed but namespace-ill-formed, so you may be able to use a non-namespace-aware XML parser to solve the problem - load the data into a DOM in non-namespace-aware mode, add the attribute "xmlns:xsi="....", and then serialize.

Most XML tools can only handle well-formed XML, so to process non-XML input you will usually need to use non-XML tools. This case however is one where you can use XML tools so long as they aren't namespace-aware.

Upvotes: 1

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

XSLT is specified as operating upon namespace-well-formed XML. Your input isn't namespace-well-formed. That means the behavior of XSLT processors when confronted with such input is out of scope for the spec, and thus it also means that while you may be able to use this or that technique with this or that XSLT processor to handle this malformed input, there is no standard way to use XSLT to do so.

Your best bet is to continue trying to figure out why your Web service result is not well formed (is it really the Web service's fault or is some software munging the data before you see it?). If all else failed, as a stopgap measure I'd use sed on the input, or the equivalent. But your mileage may vary.

Upvotes: 1

Related Questions