How to use the DOM API from XSL
Examples illustrate handy ways to use the API to manipulate the XML.
<xsl:template match="mynode">
<div onclick="doSomethingFancyWithMy(node);">
<xsl:apply-templates/>
</div>
</xsl:template>
node argument passed to the function is a reference to mynode in the XML. The node is passed as an Content Editor object. Because it is an XopusElement, it supports quite a few methods that can be used to check, change, move, extend, remove, or just change the node in some way. You can ask a lot of different things:
function doSomethingFancyWithMy(node)
{
var name = node.getNodeName();
var localname = node.getLocalName(); //in case of namespaces
var parent = node.getParentNode();
var children = node.selectNodes('*');
var samechildren = node.getChildNodes();
//but also more complicated matters:
var candelete = parent.canRemoveChild(node);
var xmlstr = node.getXML();
//finally let's do something:
node.setTextContent("Someting Fancy");
}
Xopus objects are not XML objects
Be aware that these functions do not return XML objects, but Xopus objects. Xopus objects are just the editor-specific implementation of DOM objects. For example XopusDocument is our implementation of a Document node.
for loop, instead you have to use the API to access these children as well:
var children = node.selectNodes("*");
for(i=0;i< children.getLength();i++)
{
var child = children.item(i);
}
Adding and inserting elements
var document = node.getOwnerDocument();
var newnode = document.createElement('newnode');
document.getDocumentElement().appendChild(newnode);
The above is only valid if the schema allows newnode elements at the end of the documentElement. When manipulating the tree, be aware of this.
//first let's insert it somewhere,
//so that Xopus can understand the element according to the schema
var firstChild = document.getDocumentElement().getFirstChild();
document.getDocumentElement().insertNear(newnode, firstChild);
InsertNear tries to find a suitable location for the node within the structure of the parent. It is still better to know what you are doing though. Using schema is mostly an exact science.
//now that the newnode is in the document, it can be validated
//therefore it can also be made valid:
newnode.makeComplete();
newnode have several mandatory child nodes, they will now be created by calling makeComplete. This saves you the effort of creating and appending them in order yourself. One last thing that can be quite handy:
<xsl:template match="mynode">
<div onclick="node.openAttributesEditor();">
<xsl:apply-templates/>
</div>
</xsl:template>
This simple method opens the Content Editor Attributes Editor for the specified node. If the node has no attributes nothing happens, if it does have attributes then you provide your users with the dialog. This can be quite useful when you are editing lookup values, such as images. Should you constrain the src attributes of images to a lookup, then calling the above method immediately opens the lookup also, and thus saves your users two or three clicks and mouse moves.