Possible new XProc 1.1 step: p:template

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I keep writing lots of little steps to build and insert fragments or
to restructure things.  I think I want the equivalent of a single
template + copy-all template stylesheet, something like

 <p:template match="...">
  [XML goes here, with {} interpreted in both attr vals and text content,
   and something like <c:copy select="..."/> recognised]
 </p:template>

This would allow the following simplifications (drawn from real
examples):

 Replace

  <p:insert position="first-child">
  <p:input port="insertion">
   <p:inline><Timestamp><here/></Timestamp></p:inline>
  </p:input>
 </p:insert>

 <p:string-replace match="mt:Timestamp/mt:here">
  <p:with-option name="replace" select="concat('&quot;',$stamp,'&quot;')"/>
 </p:string-replace>

 with

 <p:template match="/top">
  <top>
   <c:copy select="@*"/>
   <Timestamp>{$stamp}</Timestamp>
   <c:copy select="node()"/>
  </top>
 </p:template>

Replace

  <p:add-attribute match="c:request" attribute-name="href">
   <p:with-option name="attribute-value" select="*/@uri"/>
   <p:input port="source">
    <p:inline>
     <c:request method="GET">
      <c:header name="Accept" value="application/rdf+xml"/>
     </c:request>
    </p:inline>
   </p:input>
  </p:add-attribute>

with

 <p:template match="/*">
  <c:request method="GET" href="{@uri}">
   <c:header name="Accept" value="application/rdf+xml"/>
  </c:request>
 <p:template>

Replace

  <p:add-attribute match="/*" attribute-name="set">
   <p:with-option name="attribute-value" select="$set"/>
  </p:add-attribute>
  <p:add-attribute match="/*" attribute-name="group">
   <p:with-option name="attribute-value" select="$group"/>
  </p:add-attribute>
  <p:add-attribute match="/*" attribute-name="test">
   <p:with-option name="attribute-value" select="$test"/>
  </p:add-attribute>
  <p:add-attribute match="/*" attribute-name="validity">
   <p:with-option name="attribute-value" select="$validity"/>
  </p:add-attribute>
  <p:add-attribute match="/*" attribute-name="x:outcome" xmlns:x="x">
   <p:with-option name="attribute-value"
                  select="if ($validity=$expected) then 'win' else 'lose'"/>
  </p:add-attribute>

with

 <p:template match="/top" xmlns:x="x">
  <top set="{$set}" group="{$group}" test="{$test}" validity="{$validity}"
       x:outcome="{if ($validity=$expected) then 'win' else 'lose'}">
   <c:copy select="@*|node()"/>
  </top>
 </p:template>
 
A richer and simpler approach would be to follow Richard Tobin's lead
in one of our Edinburgh XML tools, and interpret the contents more
along the following lines:

         [interpreted as if] an XSLT template which is used to
         construct the replacement for matched nodes.

         In the stylesheet, the prefix xsl is bound to the XSL namespace.
         A low-priority template rule is included that provides an
         identity transform for nodes not matched by the template. For
         convenience, a number of entities are defined for use in the
         template:

         &this; Copies the current node and recursively processes its
                attributes and children. Equivalent to
                <xsl:copy><xsl:apply-templates select=?@*|node()?/></xsl:copy>.

         &attrs;
                Processes the attributes of the current node. Equivalent
                to <xsl:apply-templates select=?@*?/>

         &children;
                Processes the children of the current node. Equivalent to
                <xsl:apply-templates select=?node()?/>.

         &text; Copies the text of the current node. Equivalent to
                <xsl:value-of select=?.?/>.

         &space;
                Inserts a space character. Equivalent to <xsl:text>
                </xsl:text>.

         &newline;
                Inserts a newline character. Equivalent to
                <xsl:text>&#10;</xsl:text>.

This would give

 <p:template match="/top">
  <top>
   &attrs;
   <Timestamp>{$stamp}</Timestamp>
   &children;
  </top>
 </p:template>

instead, or, if we took the above literally and didn't mess with {},

 <p:template match="/top">
  <top>
   &attrs;
   <Timestamp><xsl:value-of select="$stamp"/></Timestamp>
   &children;
  </top>
 </p:template>

and so on.

Either approach would obviously be even more useful if/when we allow
node-valued variables.

ht
- -- 
       Henry S. Thompson, School of Informatics, University of Edinburgh
      10 Crichton Street, Edinburgh EH8 9AB, SCOTLAND -- (44) 131 650-4440
                Fax: (44) 131 651-1426, e-mail: ht@inf.ed.ac.uk
                       URL: http://www.ltg.ed.ac.uk/~ht/
 [mail from me _always_ has a .sig like this -- mail without it is forged spam]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFMZBufkjnJixAXWBoRAulEAJ48QTA79US7u+rUhBF1eAUpLpjrbACfS3om
/qTJGnZVd2506cZJHVbE4CE=
=7I1P
-----END PGP SIGNATURE-----

Received on Thursday, 12 August 2010 16:05:18 UTC