Suppose you want to insert images by means of custom content. Let's suggest that your schema requires the inlinegraphic element for an image within text, while when in the section element, it requires the figure element with a child called graphic.
It is possible to specify several templates for different contexts of your document. The context is specified as XSLT Match Pattern in the edit box. For each additional context you must create a separate tab in the Custom Content Dialog, specify a specific context match expression, and the content template for this content.
By default the content template will be inserted into the current cursor position. However, it may be inserted into another position relative to the current cursor position. This different position may be specified with XPath Expression in the text box and the combo-box.
See example Creating a Context-Sensitive Image Insertion Command.You can specify any number of alternative content templates. Every context, starting from the first, is tested against the
See example match expression. If the expression evaluates to true in the current context, or if there is no match expression ( field is empty), then the will be inserted. Inserting Custom Content to a Position that is Different from the Current Cursor Position.For creating a custom content described in the first paragraph of this section, do the following:
Choose
.In the Custom Content Dialog, specify:
Name: myInsertGraphic (this will identify the new command).
Inscription: Insert Graphic (this will be the human-readable inscription for the command).
Icon: browse for your favourite icon.
Hot key: hit some key combination when the cursor is in this window. For example: Ctrl-Alt-M.
You may leave the above-mentioned properties as they are provided by default.
In the
edit box, enter:<inlinegraphic fileref="$(file caption='Choose Image' filter='Image files (*.gif *.jpg);;All files (*)')"/>
text()
This means that if the match expression text() returns true for the current context (i.e. when the cursor is in the text), then the template from Custom Content Dialog should look like this:
will be inserted. TheFigure 1. First Context for a Custom Content
Now, click the
button: this will add a new context tab for the current content command.In the new tab, specify for
:<figure><title/><graphic fileref="$(file caption='Choose Image' filter='Image files (*.gif *.jpg);;All files (*)')"/></figure>
In the new tab, specify for
:section|chapter
These match expressions will return true if the cursor is immediately within the section or chapter elements. The Custom Content Dialog should look like this:
Figure 2. Second Context for a Custom Content
Sometimes you need to insert a content into a position which is different from the current cursor position. For example, usually you want to insert a new paragraph below the current paragraph, despite the fact that the cursor may be currently located within text or an emphasis element within the paragraph.
For this case you should specify the relative position where the inserted content should go. See the figure below:
In this example, the
is after , and the isancestor-or-self::para[1]
This means that the para will be inserted not into the current cursor position, but after "the first para node among all the ancestors of the current node, including the node itself". For example, if the cursor is within an emphasis of a para, the para will be found, and after it the new para will be inserted.