Working With Ephox IDs

Introduction

EditLive! for XML uses special IDs to track XML nodes as they are transformed and edited. These IDs also allow developers to access specific nodes within the document via the bean. This article will explain how Ephox IDs are used, how to determine the ID for a node and how to use them to access nodes from within the document.

How Ephox IDs Are Generated

There are three types of nodes that are assigned IDs within EditLive! for XML:

  • Elements

  • Attributes

  • Text nodes

At this stage, other types of nodes aren't manipulated by EditLive! for XML and as such are not assigned IDs. The Ephox ID for a node is assigned automatically when it is added to the document and are guaranteed to be unique within the document.

For element nodes, the Ephox ID is stored as an attribute with the name ephoxid. The Ephox ID for attributes and text nodes are based off of the parent element node's ID. For attributes, the ID is defined as the Ephox ID of the owner node with @ and the name of the attribute appended to it. For text nodes, the ID is defined as the Ephox ID of the parent node with # and the position of the text node appended to it.

Retrieving The Ephox ID Of A Node

Within An XSLT

Within an XSLT, XPath can be used to calculate the Ephox ID of a node so that it can be inserted into the generated document. This can be used to pass the Ephox ID for a node to an EventListener which can then perform custom actions using the node.

For an element, the Ephox ID can be retrieved by retrieving the ephoxid attribute. Assuming the context node is the element in question this would be expressed as:

@ephoxid

For an attribute, the Ephox ID can be retrieved by retrieving the ephoxid attribute of the owner element and appending @ followed by the local name of the attribute. Assuming the context node is the attribute in question this would be expressed as:

concat(../@ephoxid, '@', local-name())

For a text node, the Ephox ID can be retrieved by retrieving the ephoxid of the parent element and appending # followed by the position of the text node within the parent element. Assuming the context node is the text node in question this would be expressed as:

concat(../@ephoxid, '#', position())

Finally, in some cases the type of the node may not be known. The XSLT snippet below will correctly determine the Ephox ID for the context node regardless of what type the node is.

<xs:choose>
  <xs:when test="@ephoxid">
    <xs:value-of select="@ephoxid" />
  </xs:when>
  <xs:when text="not(local-name() = '')">
    <xs:value-of select="concat(../@ephoxid, '@', local-name())" />
  </xs:when>
  <xs:otherwise>
    <xs:value-of select="concat(../@ephoxid, '#', position())" />
  </xs:otherwise>
</xs:choose>

Within Java

EditLive! for XML provides a simple method for determining the ID of a given node, getEphoxIdFor(Node node). The node must already have been added to the DOM.

Retrieving A Node Using The Ephox ID

EditLive! for XML provides a method to retrieve a reference to the actual node given the Ephox ID, getNode(String ephoxID). The returned node is an instance of org.w3c.dom.Node.

See Also