xmlxsltxpath-3.0

XPath 3.0 XML parsing functions in XSLT


I have a sequence of strings that I need to parse as XML using XSLT. For example:

<span>Foo</span> & <span>bar</span> have been tagged.

When I try to parse this using fn:parse-xml-fragment (using Saxon-PE 9.6.0.5), two errors are thrown:

  1. The entity name must immediately follow the '&' in the entity reference.
  2. FODC0006: First argument to parse-xml-fragment() is not a well-formed and namespace-well-formed XML fragment.

If I remove the & entity from the input text, then it parses correctly. However, if the entity is escaped in the input, why would it cause XML parsing to fail?


Solution

  • If you want to parse XML then with XML an ampersand needs to be escaped as & and I am sure in a pure XPath context you can do parse-xml-fragment('&'). If your input is escaped inside XML then of course you need to escape an ampersand as &:

    <data>&lt;span&gt;Foo&lt;/span&gt; &amp;amp; &lt;span&gt;bar&lt;/span&gt; have been tagged.</data>
    

    or

    <data><![CDATA[<span>Foo</span> &amp; <span>bar</span> have been tagged.]]></data>