Pages: 1
Print
Author Topic: Resolve entity reference by code  (Read 3245 times)
S. Strube
Member

Posts: 9

-


WWW
« on: June 06, 2012, 02:52:56 AM »

Hi,

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?
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #1 on: June 06, 2012, 01:25:23 PM »

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?
Application.Alert(entList.length);
//If we have at least one then display info for the first...
if(entList.length > 0) {
   Application.Alert(entList.item(0).nodeName);
   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.

See:
Quote from: 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.

And:
Quote from: 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.
« Last Edit: June 06, 2012, 01:34:04 PM by Derek Read » Logged
S. Strube
Member

Posts: 9

-


WWW
« Reply #2 on: June 08, 2012, 01:16:57 AM »

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.

Quote
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.
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #3 on: June 08, 2012, 02:05:15 PM »

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
http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node-ENTITY_NODE

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.
« Last Edit: June 08, 2012, 02:09:21 PM by Derek Read » Logged
Pages: 1
Print
Jump to:  

email us