Is there an easy way to use XSLT to detect and cor

2019-07-04 12:22发布

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 processing. Anyone know a way around this?

Thanks!

标签: xml xslt
4条回答
Root(大扎)
2楼-- · 2019-07-04 12:30

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

查看更多
贪生不怕死
3楼-- · 2019-07-04 12:30

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.

查看更多
贪生不怕死
4楼-- · 2019-07-04 12:41

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.

查看更多
趁早两清
5楼-- · 2019-07-04 12:48

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.

查看更多
登录 后发表回答