1

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?

tat
  • 321
  • 1
  • 19
  • Can you please add a minimal but complete snippet that causes that error? If it is pure XPath then I don't see why the string would have any escaped markup at all, if it is inside XML or XSLT then obviously you would have a single `&` passed to the XML parser and XML does not allow that, you need it escaped as `&` or inside XML as `&`. – Martin Honnen Nov 14 '16 at 18:59

1 Answers1

3

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>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • That was it--I needed to ensure that the `&` entities (inside XML) were escaped as `&amp;`. – tat Nov 14 '16 at 19:49