Tench
Tench

Reputation: 525

Passing document href as an option in XProc

I have an XProc script that currently hardcodes document hrefs like this:

<p:xslt>
    <p:input port="source"/>
    <p:input port="stylesheet">
        <p:document href="/path/to/stylesheet.xsl"/>
    </p:input>
</p:xslt>

I would like to pass /path/to/stylesheet.xsl as an option to the xproc script, so in my declare-step I put:

<p:option name="stylePath" required="true"/>

but I still can't figure out how to replace the hardcoded href in p:document with the option value.

I'll be most grateful for your help.

Upvotes: 0

Views: 74

Answers (2)

Martin Kraetke
Martin Kraetke

Reputation: 16

In contrast to version 1.0, XProc 3.0 allows you to work with Attribute Value Templates (AVT). AVTs are set in curly braces and they indicate that the option value should be evaluated as XPath expression. Here is an XProc 3.0 pipeline where the option $stylesheet-path is set as an AVT to be evaluated dynamically:

<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">
  
  <p:input port="source"/>
  
  <p:output port="result"/>
  
  <p:option name="stylesheet-path"/>
  
  <p:xslt>
    <p:with-input port="stylesheet" href="{$stylesheet-path}"/>
  </p:xslt>
  
</p:declare-step>

Upvotes: 0

Tench
Tench

Reputation: 525

Here's what eventually worked for me:

  1. Create a step to load the document with the option passed to it:

    <p:load name="getStylesheetSource">
      <p:with-option name="href" select="$stylePath" /> 
    </p:load>
    
  2. Then, in the XSLT step, pipe the loaded stylesheet

    <p:xslt name="transform">
        <p:input port="source">
           <!-- etc. -->  
        </p:input>
        <p:input port="stylesheet">
           <p:pipe step="getStylesheetSource" port="result" /> 
        </p:input>
    </p:xslt>
    

With this setup, one can pass the stylePath option when invoking the XProc script without having to hard code the path.

Upvotes: 1

Related Questions