General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion DOMElement xml property in XMAX 5.5

  • craig_83

    DOMElement xml property in XMAX 5.5

    Participants 2
    Replies 3
    Last Activity 12 years, 10 months ago

    The 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.

    Reply

    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

    craig_83

    Reply to: DOMElement xml property in XMAX 5.5

    The 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 Author

    Access
    Read only

    Returns
    String

    Usage in JScript
    Document_object.documentElement.xml;

    Usage in VBScript
    Document_object.documentElement.xml

    Example
    // 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?

    Reply

    Derek Read

    Reply to: DOMElement xml property in XMAX 5.5

    This 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)
    xml

    Applies To
    XMetaL® XMAX™ and XMetaL® Author

    Access
    Read only

    Returns
    String

    Description
    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.xml

    Example
    // XMetaL Script Language JSCRIPT:
    // retrieve the entire document into a string
    var doc = ActiveDocument.xml;
    ActiveDocument.Host.Alert(doc);

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support