General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › DOMElement xml property in XMAX 5.5
-
craig_83 December 10, 2009 at 1:02 pm
DOMElement xml property in XMAX 5.5
December 10, 2009 at 1:02 pmParticipants 2Replies 3Last Activity 13 years, 1 month agoThe help file suggests that I can get xml of the current document element
“Xml: Returns the XML information set for a given DOMElement, including default or fixed attribute values.
Application.Alert(ActiveDocument.documentElement.xml);”
However, I can only see .xml when related to a the xml “Document” as a whole.
It would be really useful to access a single element and it's children as a string, rather than trying to traverse the DOM of a single element.
Derek Read December 15, 2009 at 1:46 am
Reply to: DOMElement xml property in XMAX 5.5
December 15, 2009 at 1:46 amI'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 firstelement anywhere in the document
var myNodeList = ActiveDocument.getNodesByXPath("//title[1]");
if (myNodeList.length > 0) {
myNode = myNodeList.item(0);
Application.Alert("Node Name: " + myNode.nodeName);
Application.Alert("Node Type: " + myNode.nodeType);
if (myNode.firstChild.nodeType == 3) { // 3 = DOMtext
Application.Alert("Node Value: " + myNode.firstChild.nodeValue);
}
else {
Application.Alert("Node content (first child) is not text.");
}
}
else {
Application.Alert("Didn't find ain this document.");
}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 firstelement anywhere in the document
var myNodeList = ActiveDocument.getNodesByXPath("//title[1]");
if (myNodeList.length > 0) {
myNode = myNodeList.item(0);
//create a Range object to manipulate
rng = ActiveDocument.Range;
//convert the node into a selection (using Range)
rng.SelectNodeContents(myNode);
//we have the element's inner content, select the whole element
rng.SelectElement();
//show the XML source for the selection
Application.Alert(rng.Text);
}
else {
Application.Alert("Didn't find ain this document.");
}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.
craig_83 December 15, 2009 at 8:27 am
Reply to: DOMElement xml property in XMAX 5.5
December 15, 2009 at 8:27 amThe topic can be found by searching “xml” and it is the third search result listed. It states:
xml
Returns the XML information set for a given DOMElement, including default or fixed attribute values.Applies to
XMetaL XMAX and XMetaL AuthorAccess
Read onlyReturns
StringUsage in JScript
Document_object.documentElement.xml;Usage in VBScript
Document_object.documentElement.xmlExample
// XMetaL Script Language JSCRIPT:
Application.Alert(ActiveDocument.documentElement.xml);It gives me the impression that I could potentially pull xml fragments from a given element?
Derek Read December 16, 2009 at 1:01 am
Reply to: DOMElement xml property in XMAX 5.5
December 16, 2009 at 1:01 amThis is a documentation error in the 5.5 release. The documentElement object is basically the same as the DOMElement object but specific to the root node of the document. DOMElement inherits the domNode interface which does not include an “xml” property.
The sample code that is listed for the topic you found is misleading and incorrect and the topic itself should not exist as far as I can tell. I'm guessing someone probably mistakenly duplicated (and messed up) the documentation for Document_object.xml, which provides an easy method for obtaining the xml source for the entire document as a string (but not at any level below that for which you should have a look at the previous examples I've posted in this thread). Here are the docs for Document_object.xml:
xml (Document property)
xmlApplies To
XMetaL® XMAX™ and XMetaL® AuthorAccess
Read onlyReturns
StringDescription
You can access the entire XML/SGML document (with DOCTYPE and XML PI) and store the information in a variable by using Document.xml. One example is when XMetaL is the front end of a larger solution with other applications interacting with it. For instance, in a publishing house, there are times when an author wants to preview XML or SGML documents in a third-party application. With this property, XMetaL can hand off the entire document, including the DOCTYPE, to the third party application. If you are scripting for XMetaL XMAX, see also XMetaL XMAX Document.xml.If you want to return the entire document with Change Tracking Processing Instructions intact, see the property Document.xmlWithCT.
Usage in JScript
var strDoc = ActiveDocument.xml;Usage in VBScript
dim strDoc = ActiveDocument.xmlExample
// XMetaL Script Language JSCRIPT:
// retrieve the entire document into a string
var doc = ActiveDocument.xml;
ActiveDocument.Host.Alert(doc); -
AuthorPosts
- You must be logged in to reply to this topic.