General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Resolve entity reference by code

  • S. Strube

    Resolve entity reference by code

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


    I want to resolve entity references used in a text I get by Range.Text.

    Therefore I iterate the Application.ActiveDocument.doctype.entities and get the names (entity.nodeName) of the entities, but how do I resolve the value from here?

    Is there any other solution than parsing the DTD manually?


    Derek Read

    Reply to: Resolve entity reference by code

    When you say you want to “resolve” the entity, what exactly do you mean? Does this mean you want to extract the value defined for the entity? I'm assuming we're talking about text entities. What do you need to do with the value?

    If DOM supported it I think you would use nodeValue. The following shows some code that will not work (and I have nothing better to offer at the moment).

    //XMetaL Script Language JScript:
    var entList = ActiveDocument.doctype.entities;
    //How many entities are defined?
    //If we have at least one then display info for the first...
    if(entList.length > 0) {
    Application.Alert(entList.item(0).nodeType);  //<--this should be 6
    Application.Alert(entList.item(0).nodeValue); //<--this should return null

    That returns null for nodeValue because DOM (the spec) didn't define that for nodes of type entity (type 6) and XMetaL has not added an extension to the DOM to do that either.

    [quote=XMetaL Developer Programmers Guide for 'nodeValue']The DOM specification details which types of nodes have values, and where the values come from:

    DOMAttr: the attribute value
    DOMText: the text
    DOMCDATASection: content of the section
    DOMProcessingInstruction: everything in the PI except the target.
    DOMComment: content of the comment.
    DOMCharacterReference: the decimal number corresponding to the character (extension to DOM)
    All other nodes: null.
    Set applies only to nodes whose value is not null by definition.

    [quote=XMetaL Developer Programmers Guide for 'nodeType']The allowed values are specified by the DOM specification:

    1: DOMElement
    2: DOMAttr
    3: DOMText
    4: DOMCDATASection
    5: DOMEntityReference
    6: DOMEntity
    7: DOMProcessingInstruction
    8: DOMComment
    9: Document (DOMDocument)
    10: DOMDocumentType
    11: DOMDocumentFragment
    12: DOMNotation
    505: DOMCharacterReference (extension to DOM)

    Perhaps knowing what your ultimate goal is will allow me to look at alternative options.


    S. Strube

    Reply to: Resolve entity reference by code

    Yes, I'm talking about text entities and I mean to extract the value defined.

    The use case is an authoring assistance we have, which queries the sentence the user currently writes in a database to find same and similar sentences as a suggestion for auto completion and consistent writing.

    To do this, we're indexing the CMS texts with resolved text entities in the database – means without entities like &productName;. So I need to query these sentences in the same manner.

    I'd also would have expected nodeValue to be the value of the entity, but as you've written the example does not work.

    Current workaround is to use an external process that reads the DTD URL from the active document, creates an XML document from the fragment in the selected range and passes this through an XML reader to resolve the entities.

    and I have nothing better to offer at the moment

    Hence I think we need to stick with the workaround for this.
    Please notify me, if you find a beter in-app / API solution for this.


    Derek Read

    Reply to: Resolve entity reference by code

    Our APIs are just following the W3C DOM (as noted in the Programmers Guide). The W3C DOM outlines the expected return values for different node types in a table on this page. See: The values of nodeName, nodeValue, and attributes vary according to the node type as follows

    So, if you end up using an XML parser (or similar) that gives you what you need please let us know. Perhaps we could use its behaviour as a model.


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

Lost Your Password?