XML Processing Fish logo

XProc FAQ

This page summarizes some of the Frequently Asked Questions about XProc. Feel free to send more (with answers, if you can :-).

Why doesn't p:string-replace work?

Why does p:string-replace report an XPath exception when I try to use a variable in the replacement text?

<p:variable name="value" select="/some/expression"/>

<p:string-replace match="//target" replace="concat('X-',$value)"/>

XProc steps don't have access to the options or variables defined in your pipeline (nor do they have access to XPath extension functions defined by XProc).

If you want to use the value of a variable, or an XPath extension function, in the computed value of your step, you must let the XProc processor compute the value:

<p:variable name="value" select="/some/expression"/>

<p:string-replace match="//target">
  <p:with-option name="replace" select="concat('&quot;X-',$value,'&quot;')"/>
</p:string-replace>

Why does p:string-replace report an XPath exception when I pass a computed expression in the replacement text?

<p:string-replace match="//target">
  <p:with-option name="replace" select="p:base-uri()"/>
</p:string-replace>

The p:string-replace step evaluates the value of the replace option as an XPath expression.

In the example above, when p:string-replace matches a //target, it attempts to evaluate the value of the base URI as an XPath expression and that's (not likely to be) a syntactically correct expression.

When you pass a computed value to p:string-replace, you almost always want to explicitly add quotes to the computed value so that when it's evaluated by the step, it's simply a string literal:

<p:string-replace match="//target">
  <p:with-option name="replace" select="concat('&quot;',p:base-uri(),'&quot;')"/>
</p:string-replace>