0

I can not create a XSL nodeset from a string. I can create a nodeset from a Result Tree Fragment. This stylesheet shows both attempts.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
version="1.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:variable name="rtf">
      <root>
          <child>
          </child>
      </root>
  </xsl:variable>

  <xsl:variable name="rtfNs" select="exsl:node-set($rtf)" />

  <xsl:variable name="str" select="'&lt;root&gt;&lt;child&gt;&lt;/child&gt;&lt;/root&gt;'" />

  <xsl:variable name="strNs" select="exsl:node-set($str)" />

  <xsl:template match="/">
    <xsl:copy-of select="$rtfNs" />
    <xsl:copy-of select="$rtfNs/root/child" />
    <xsl:copy-of select="$strNs" />
  </xsl:template>

</xsl:stylesheet>

Produces

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:exsl="http://exslt.org/common">
   <child/>
</root>
<child xmlns:exsl="http://exslt.org/common"/>&lt;root&gt;&lt;child&gt;&lt;/child&gt;&lt;/root&gt;

The code shows a nodeset can be created from an RTF and used in a select attribute. The attempt to create a nodeset from a string results in a string with embedded entities (i.e. &lt; and &gt;). The string can not be defined without using the entities. This question is a simplification of an attempt to pass a external string into the stylesheet from .NET System.Xml.Xsl and convert that string into a nodeset.

DSHCS
  • 11
  • 1
  • 3
  • 1
    If you're using XSLT 1.0, and your input is a string containing escaped XML, you need to do the transformation in two passes: in the first pass, use `disable-output-escaping` to unescape the string and save the result to a file; in the second pass process the resulting file using another XSLT stylesheet. – michael.hor257k Sep 03 '19 at 21:13
  • Just a fine point, the passed string did not contain escaped characters. I did an experiment using XPATH translate() and the characters that get output escaped are not escaped in the passed string. They get escaped on output (xsl:copy-of or value_of) and the node-set function must return a string instead of a node-set and that gets escaped on output. – DSHCS Sep 03 '19 at 22:01
  • I am afraid you're missing the point: your `$str` variable contains escaped characters. Similarly, if you pass the same string as a parameter, or get it from a CDATA section in the input XML, the XML parser will interpret it as a string, not markup. A string cannot contain the `<` character, unless it's escaped. The `node-set()` function is completely irrelevant here. – michael.hor257k Sep 03 '19 at 22:11

1 Answers1

0

I did get a solution on the .NET side, but want to get the XSL side responses too...

Dim objXmlDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
objXmlDoc.LoadXml("<root><child></child></root>")
Dim objXmlNav As System.Xml.XPath.XPathNavigator = objXmlDoc.CreateNavigator()
objXmlNav.MoveToRoot()

Dim objValidationArgList As New System.Xml.Xsl.XsltArgumentList
objValidationArgList.AddParam("test", "", objXmlNav)

This passed an RTF into the stylesheet and when processed with node-set($test) resulted in a nodeset that could be used just like one created from an internal (xsl:variable) RTF.

DSHCS
  • 11
  • 1
  • 3