XSLT FAQ

How to parse an XHTML value from a data field used in XSLT functions?

One of the fields in my data type contains XHTML. When I use its value in XSLT like <xsl:copy-of select="@LongDescription"> I get raw XHTML in the output.

How can I have this XHTML parsed to get 'bold text' rather than '<b>bold text</b>'?

Answer:

If you have a data field containing XHTML, and emit it from a C1 function (a C# function returning XhtmlDocument, an XSLT function, etc), you should first parse the XHTML.

In an XSLT function, you should use built-in parser functions available via the XSLT MarkupParser extension.

In your XSLT function:

  1. Add a function call to the Composite.Xslt.Extensions.MarkupParser function.
  2. Use one of the two markup parser function with the XHTML value, for example:
<xsl:copy-of select="Parser:ParseXhtmlBodyFragment(@LongDescription)" 
             xmlns:Parser="#MarkupParserExtensions" 
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />

Please note that you should use <xsl:copy-of/> to get the value returned by one of the parser functions unchanged. (If you use <xsl:value-of/>, the value will be stripped of the formatting tags.)

When you add a call to the MarkupParser function, two parser functions become available in your XSLT:

  • XPathNavigator ns:ParseWellformedDocumentMarkup(String wellformedMarkupString)
  • XPathNavigator ns:ParseXhtmlBodyFragment(String xhtmlBodyFragmentString)

Please make sure to use the '#MarkupParserExtensions' namespace when calling either function. You can either use it inline like in the sample above or add it to the <xsl:stylesheet /> element.