46. xpath

xpath(XPath_expression)

Generalization of standard construct attr(attribute_name). Inserts in generated content the value of XPath_expression, an XPath 1.0 expression using the target of the CSS rule (element, comment or processing instruction) at its context node.

Example:

xpath("id(@linkend)/@xreflabel")

Note that xpath(), like attr(), is evaluated once and this happens when the view of the element is built. This means that in most cases, manually refreshing the view of the element after a change in the document will be needed (use SelectRedraw (Ctrl+L)).

Specifying attr(foo) in a CSS rule implicitly creates a dependency between the value of attribute foo and the element which is the target of the CSS rule: the view of the element is automatically rebuilt when the value of its attribute foo is changed.

Similarly, specifying xpath(whatever) in a CSS rule implicitly creates a dependency between the element which is the target of the CSS rule and all its attributes: the view of the element is automatically rebuilt when the value of any of its attributes is changed (which too much or not enough depending on the value of the whatever XPath expression!).

See also label().

Tip

You are not restricted to the standard functions of XPath 1.0. A few XSLT 1.0 functions such as document() are also supported, as well as many very useful extension functions documented in Section 1, “Extension functions” in XMLmind XML Editor - Support of XPath 1.0.

About the document() function when used in the context of a CSS stylesheet

Example: let's file list.xml contain a list of admonition types:

<list>
  <item>note</item>
  <item>important</item>
  <item>caution</item>
  <item>warning</item>
</list>

The CSS file contains the following rule:

warning:before {
  content: combo-box(attribute, type,
                     values,
                     xpath("join(document('list.xml')//item, '\A')"));
}

Because file list.xml is found in the same css/ directory as the CSS stylesheet, you'll expect the above invocation of document() to work fine. The problem is that the above invocation of document() will not work.

Expression document('list.xml') means: load file list.xml, where URI list.xml is resolved against the URI of the XML node containing the invocation of document(). A CSS file does not contain XML, hence there is no XML node containing the invocation of document().

One way to make the above example work is to replace document('list.xml') by document(resolve-uri('list.xml', $styleSheetURL)). Variable $styleSheetURL is automatically set by XXE during the evaluation of an XPath expression in the context of a CSS stylesheet and contains the URL of this CSS stylesheet.

Another way to make the above example work is to replace document('list.xml') by document('my-config:css/list.xml'), where the "my-config:" URI prefix is defined as follows in the XML catalog of your custom XXE configuration:

<rewriteURI uriStartString="my-config:" rewritePrefix="." />

More information about such custom XML catalogs in XML catalogs in XMLmind XML Editor - Configuration and Deployment.