1

I have an XML that contains a special character (&). The outputted value is obtained from a database and needs to remain as it is.

Below is an example of the XML

<Data>
   <fname>Patrick</fname>
   <lname>Lewis</lname>
   <resortloc>the pearl & shine</resortloc>
</Data>

The issue here is the &. Which is throwing an error -

An error occurred while parsing EntityName.

I also have a C# function that I created, to replace the & character with a valid character see below. But when I run the code I still get the error. Could someone please help with this. Thank you

public static string EscapeXMLValue(string b)
{
   return b.Replace( "&","&amp;");
}

Below is my XSLT code

<xsl:value-of select= "cs:EscapeXMLValue(resortloc)" /> 

Please help. thank you.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • You need to replace this "&" with "and" ?, but you still using it in Replace( "&","&") just do this return b.Replace( "&","and"); – Usama Safi Oct 06 '20 at 02:37
  • 2
    You say you "have an XML", but you don't. You have a non-XML. XSLT can only process well-formed XML. – Michael Kay Oct 06 '20 at 06:39
  • You are doing something very wrong if you are trying to manually escape your string values in XSL. XSL is going to produce valid properly escaped XML by default if you don't stand in its way. – GSerg Oct 06 '20 at 08:24

4 Answers4

1

I have an XML that contains a special character ("&").

No, you do not have XML. "XML" is not well-formed (and therefore not XML) if it has an & in a text node that's not part of an entity reference. For the ampersand character itself, use &amp;.

The outputted value is obtained from a database and needs to remain as it is.

Doesn't matter where it came from – it's not XML, and you cannot use XML tools or libraries to process it. Either fix the database export, manually fix the data, or try to automatically pre-process the data as text prior to passing it to any XML libraries or tools.

See also:

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

You won't be able to use an XML parser on your markup with the ampersand character.. As I understand .net's XML stack, using a custom function from XSLT isn't going to work either, since the error is emitted before the function gets to see the text data. If you can't modify your input at the source, you might have luck using an SGML parser as a preprocessor to convert your input to well-formed XML. Specifically, the venerable SP/OpenSP SGML suite contains the osx program for this very purpose. Though I'm not sure there's a recent Windows build.

imhotap
  • 2,275
  • 1
  • 8
  • 16
0

In the .NET world, not as part of Microsoft's .NET framework library, but on NuGet, there is SgmlReader https://www.nuget.org/packages/Microsoft.Xml.SgmlReader/ that can help parse such input and pass it on to most .NET XML APIs expecting an XmlReader like XPathDocument, XmlDocument, XDocument or XslCompiledTransform.

So running e.g.

        SgmlReader sgmlReader = new SgmlReader();
        sgmlReader.Href = Environment.CurrentDirectory + @"\your-not-well-formed-sample.xml";

        XPathDocument document = new XPathDocument(sgmlReader);

        Console.Out.WriteLine(document.CreateNavigator().OuterXml);

gives

<Data>
  <fname>Patrick</fname>
  <lname>Lewis</lname>
  <resortloc>the pearl &amp; shine</resortloc>
</Data>

meaning SgmlReader could be used to pass your input to XSLT.

So one approach to handling your input is to use SgmlReader as the parser before XSLT processes the input.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
-1

Try this, referenced by MS Docs:

public static string EscapeXMLValue(string tagText)
{
   return SecurityElement.Escape(tagText);
}