This function can be called from DirXML Script, and offers a reliable way to evaluate XPATH expressions from within the context of the current XDS document.
<do-set-local-variable name="varValue" scope="policy">
<arg-node-set>
<token-xpath expression="es:getXPATH( $varDoc, $varXPATH )"/>
</arg-node-set>
</do-set-local-variable>
function getXPATH ( varDoc, varExpression )
{
var varLevel = 3;
tracer.trace(" getXPATH ( doc ) =====>> " + varDoc, varLevel);
tracer.trace(" getXPATH ( expression ) =====>> " + varExpression, varLevel);
var nodeSet = new Packages.com.novell.xml.xpath.NodeSet();
var document = Packages.com.novell.xml.dom.DocumentFactory.newDocument();
var ndsElement = document.createElement("nds");
document.appendChild(ndsElement);
ndsElement.setAttributeNS(null, "dtdversion", "3.5");
var outputElement = document.createElement("output");
ndsElement.appendChild(outputElement);
var instanceElement = document.createElement("instance");
try
{
var docFactory = DocumentBuilderFactory.newInstance();
var docBuilder = docFactory.newDocumentBuilder();
var xPathFactory = XPathFactory.newInstance().newXPath();
var thisDoc = docBuilder.parse( new org.xml.sax.InputSource( new StringReader( varDoc ) ) );
var thisDocRoot = thisDoc.getDocumentElement();
var thisExpression = xPathFactory.compile( varExpression );
var thisValue = xPathFactory.compile( "." );
var thisResult = thisExpression.evaluate( thisDocRoot, XPathConstants.NODESET );
var varResult_Length = thisResult.getLength();
var attrElement = document.createElement("attr");
for ( len = 0; len < varResult_Length; len++ )
{
singleNode = thisResult.item( len ).cloneNode(true);
var myValue = thisValue.evaluate(singleNode, XPathConstants.STRING);
tracer.trace(" getXPATH ( value " + Number(len + 1) + " ) =====>> " + myValue, varLevel);
var valueElement = document.createElement("value");
valueElement.appendChild(document.createTextNode(myValue));
attrElement.appendChild(valueElement);
}
}
catch (err)
{
var attrElement = document.createElement("attr");
var myValue = thisExpression.evaluate( thisDocRoot, XPathConstants.STRING );
tracer.trace(" getXPATH ( value 1 ) =====>> " + myValue, varLevel);
var valueElement = document.createElement("value");
valueElement.appendChild(document.createTextNode(myValue));
attrElement.appendChild(valueElement);
}
finally
{
instanceElement.appendChild(attrElement);
outputElement.appendChild(instanceElement);
nodeSet.add(instanceElement);
return nodeSet;
}
}