开发者

Is there an easy way to use XSLT to detect and correct not well-formed XML?

For example:

<doc xmlns="http://www.foo.org">
  <div id>
    <title>Mr. Title</title>
    <paragraph>This is one paragraph.</paragraph>
  </div>
</doc>

Note that the div tag has an attribute id with no value assigned. I would like to correct it with XSLT, but when I apply the XSL to this XML, it errors "XML Parsing Error: not well-formed" before pro开发者_运维问答cessing. Anyone know a way around this?

Thanks!


What you want is not possible. Even though the result of XSL application does not have to be well formed, the input to it must be.


The answers saying that a prerequisite for the start of the XSLT transformation is to have a well-formed (and parsed) XML document, are correct.

However:

  1. In XSLT 2.0 having a source XML document is not required.

  2. Both XSLT 1.0 and XSLT 2.0 leave to the implementation what to do in case the document() function cannot parse the file identified by the URI (constructed from the) argument. If your XSLT processor does not raise an error and just retutns an empty node-set, this can be used as a technique to determine if a file identified by a particular URI is well-formed XML document or not.


No. The XML must be parsed before XSLT can be applied to it.


In XSLT 2.0, you can use the doc-available() function, which returns false if no resource with the given URI exists or if the resource exists but is not XML. If you know what kind of repair work is needed you could then attempt it by reading the resource using the unparsed-text() function and manipulating the result as text. In general, though, if someone is sending you bad XML then the recommended approach is to persuade them to stop doing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜