Novell Home

Converting Strings to Nodesets

Novell Cool Solutions: Tip

Digg This - Slashdot This

Posted: 21 Jul 2005
 

Problem

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 
xmlns:xdoc="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XmlDocument">
<rule>
<description>Load filter into a local variable: method 1</description>
<conditions>
<and/>
</conditions>
<actions>
<do-set-local-variable name="stringxml">
<arg-string>
<token-attr name="whatever"/>
</arg-string>
</do-set-local-variable>
<do-set-local-variable name="nodesetxml">
<arg-node-set>
<token-xpath expression="xdoc:getDocument(xdoc:new($stringxml))"/>
</arg-node-set>
</do-set-local-variable>
</actions>
</rule>
</policy>

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="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:pfac="http://www.novell.com/nxsl/java/com.novell.xml.parser.XMLParserFactory"
xmlns:parser="http://www.novell.com/nxsl/java/com.novell.xml.parser.XMLParser"
xmlns:srdr="http://www.novell.com/nxsl/java/java.io.StringReader"
>

<!-- 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 
see http://developer.novell.com/ndk/doc/dirxml/dirxmlbk/api/com/novell/xml/parser/XMLParserFactory.html
-->
<xsl:variable name="parser" select="pfac:newParser()"/>
<!--
parse the string, get node-set back containing the root of the document
see http://developer.novell.com/ndk/doc/dirxml/dirxmlbk/api/com/novell/xml/parser/XMLParser.html#parse(java.io.Reader)
-->
<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"/>
</xsl:template>

</xsl:transform>


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

© 2014 Novell