Re: [xsl] preceding axis is too permissive for my stylesheet

Subject: Re: [xsl] preceding axis is too permissive for my stylesheet
From: "James A. Robinson" <jim.robinson@xxxxxxxxxxxx>
Date: Sun, 01 Jun 2008 09:06:36 -0700
>     <div1><!--Article-->
>         <ptr target="ids.27.3.3fm" n="9"/>
>         <ptr target="ids.27.3.4fm" n="10"/>
>         <div2><!--Notes-->
>             <ptr target="ids.27.3.3tm" n="1"/>
>             <ptr target="ids.27.3.4tm" n="2"/>
>         </div2>
>     </div1>
>     <test><ptr/></test>
> 
> This not quite what I want. In the second <div1>, I want the first two
> @n values to be "1" and then "2", to match the markers in "Notes". The

So basically you want to renumber the @n value by the number of 'fm'
ptrs which occur along the ancestor axis and which precede the current
ptr?

If I understand what you're asking for, the ancestor axis paired with the
'<<' comparision operator might be one what do get the counts you want:

  ancestor::*/ptr
    [matches(@target, '([a-z]{3})\.([0-9]+)\.([0-9s]+)\.([0-9]+)fm')]
    [. &lt;&lt; current()]

For all ancestors which contain a ptr matching the 'fm' pattern, select
those matching ptr elements if they occur before the current context.

An example stylesheet (I used a few variables and keys to make it easier
for me to work with):

l:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:tei="uri:some-made-up-namespace"
  exclude-result-prefixes="tei xs">

  <!-- regex to match ptr/@target -->
  <xsl:variable name="tei:ptr-fm-regex" as="xs:string"
    select="'([a-z]{3})\.([0-9]+)\.([0-9s]+)\.([0-9]+)fm'"/>
  <xsl:variable name="tei:ptr-tm-regex" as="xs:string"
    select="'([a-z]{3})\.([0-9]+)\.([0-9s]+)\.([0-9]+)tm'"/>
  
  <!-- Locate ptr, with a @target matching $tei:ptr-fm-regex, by value @n -->
  <xsl:key name="tei:ptr-fm-by-n" match="ptr[matches(@target, $tei:ptr-fm-regex)]" use="@n"/>

  <!-- return a count of one + the number of fm ptr elements along the ancestor axis
       which precede $ptr. -->
  <xsl:function name="tei:ptr-fm-count">
    <xsl:param name="ptr" as="element(ptr)"/>
    <xsl:sequence select="1+count($ptr/ancestor::*/ptr[matches(@target, $tei:ptr-fm-regex)][. &lt;&lt; $ptr])"/>
  </xsl:function>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- For each 'fm' ptr, copy it, but replace @n with the tei:ptr-fm-count value -->
  <xsl:template match="ptr[matches(@target, $tei:ptr-fm-regex)]">
    <ptr target="{@target}" n="{tei:ptr-fm-count(.)}" />
  </xsl:template>

  <!-- For tm ptr elements look up the matching fm ptr by @n and return its
       tei:ptr-fm-count.  Of course, we're replacing @n now with non-unique
       values, so this particular lookup won't be any good for our output
       document.  I'd have used @target, but that doesn't look to be unique. -->
  <xsl:template match="ptr[matches(@target, $tei:ptr-tm-regex)]">
    <ptr target="{@target}" n="{tei:ptr-fm-count(key('tei:ptr-fm-by-n', @n))}" />
  </xsl:template>

</xsl:stylesheet>


Produces:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <div1><!--Article-->
        <ptr target="ids.27.3.1fm" n="1"/>
        <ptr target="ids.27.3.2fm" n="2"/>
        <div2><!--Subsection-->
            <ptr target="ids.27.3.3fm" n="3"/>
            <ptr target="ids.27.3.4fm" n="4"/>
        </div2>
        <div2><!--Notes-->
            <ptr target="ids.27.3.1tm" n="1"/>
            <ptr target="ids.27.3.2tm" n="2"/>
            <ptr target="ids.27.3.3tm" n="3"/>
            <ptr target="ids.27.3.4tm" n="4"/>
        </div2>
    </div1>
    <div1><!--Article-->
        <ptr target="ids.27.3.3fm" n="1"/>
        <ptr target="ids.27.3.4fm" n="2"/>
        <div2><!--Notes-->
            <ptr target="ids.27.3.3tm" n="1"/>
            <ptr target="ids.27.3.4tm" n="2"/>
        </div2>
    </div1>
    <test><ptr/></test>
</root>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson@xxxxxxxxxxxx
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

Current Thread