Home Forums General XMetaL Discussion DOMElement xml property in XMAX 5.5 Reply To: DOMElement xml property in XMAX 5.5

Derek Read

Reply to: DOMElement xml property in XMAX 5.5

I'm not sure which version of the Programmer's Guide you are looking at as I don't see this particular description, though perhaps I'm looking in the wrong places in my copy (distributed with XMetaL Developer 5.5). Can you explain how to get to the topic you are looking at so we can make sure it makes sense?

You can use the DOM nodeValue property to obtain (and if allowed, set) the value of a particular node, keeping in mind that some node types like DOMelement will return null for their nodeValue (so perhaps not very useful depending on what you are doing):

//XMetaL Script Language JScript:
//make our node equal to the first element anywhere in the document<br />var myNodeList = ActiveDocument.getNodesByXPath("//title[1]");<br />if (myNodeList.length > 0) {<br /> myNode = myNodeList.item(0);<br /> Application.Alert("Node Name: " + myNode.nodeName);<br /> Application.Alert("Node Type: " + myNode.nodeType);<br /> if (myNode.firstChild.nodeType == 3) { // 3 = DOMtext<br /> Application.Alert("Node Value: " + myNode.firstChild.nodeValue);<br /> }<br /> else {<br /> Application.Alert("Node content (first child) is not text.");<br /> }<br />}<br />else {<br /> Application.Alert("Didn't find a in this document.");<br />}

You'll see that you aren't going to be able to easily return the underlying XML source this way however, as you can only grab bits and pieces.

If you just need the XML source for a particular fragment of a document, and depending on how you like to work and how the rest of your code is written you may find it easier to use the Selection.Text property (or a Range object instead of Selection), as follows:

//XMetaL Script Language JScript:
//make our node equal to the first element anywhere in the document<br />var myNodeList = ActiveDocument.getNodesByXPath("//title[1]");<br />if (myNodeList.length > 0) {<br />  myNode = myNodeList.item(0);<br />  //create a Range object to manipulate<br />  rng = ActiveDocument.Range;<br />  //convert the node into a selection (using Range)<br />  rng.SelectNodeContents(myNode);<br />  //we have the element's inner content, select the whole element<br />  rng.SelectElement();<br />  //show the XML source for the selection<br />  Application.Alert(rng.Text);<br />}<br />else {<br />  Application.Alert("Didn't find a in this document.");<br />}

Note: If your users use the Revision Marking feature (aka: Change Tracking) and you wish to see that markup then you need to use Selection.TextWithRM (or the same property on a Range object).

If these suggestions don't get you what you need a higher level description of what you are trying to do might help us to help you.

Reply

Products
Downloads
Support