Novell Home

Converting Strings to Nodesets

Novell Cool Solutions: Tip

Digg This - Slashdot This

Posted: 21 Jul 2005


I have an eDirectory string attribute that contains an XML fragment. I'd like to use Xpath to extract information, but it is a string, not a nodeset. How can I convert from a string to a nodeset?

Perin Blanchard: Assuming you attribute is a well-formed XML document and therefore can be parsed, the following is one technique you can use:

<?xml version="1.0" encoding="UTF-8"?><policy 
<description>Load filter into a local variable: method 1</description>
<do-set-local-variable name="stringxml">
<token-attr name="whatever"/>
<do-set-local-variable name="nodesetxml">
<token-xpath expression="xdoc:getDocument(xdoc:new($stringxml))"/>

Father Ramon: The following stylesheet is an example of one way to do it without writing any Java code yourself.

<?xml version="1.0" encoding="UTF-8"?>

<!-- this stylesheet demonstrates parsing an XML document from a string -->

<xsl:transform xmlns:xsl="" version="1.0"

<!-- This is for testing the stylesheet outside of DirXML so things are pretty to look at -->
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="value component password check-password"/>
<xsl:output indent="yes" method="xml"/>

this variable ends up with a string that can be parsed as XML.
(technically, it has a result-tree-fragment with a single text node
containing a string, but when you take the string value...)
<xsl:variable name="test-string"><parent><child attr="whatever"/></parent></xsl:variable>

<xsl:template match="/">
create an instance that implements XMLParser 
<xsl:variable name="parser" select="pfac:newParser()"/>
parse the string, get node-set back containing the root of the document
<xsl:variable name="data" select="parser:parse($parser, srdr:new(string($test-string)))"/>
demonstrate using XPath on the result of the parse
<xsl:copy-of select="$data/parent/child"/>


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions.

© 2014 Novell