Re: [xsl] Correcting a prior error

Subject: Re: [xsl] Correcting a prior error
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 30 Jul 2010 10:00:30 +0100
I think you're out by one: the outermost element will have

count(ancestor::*|preceding::*)

equal to 0, but can be retrieved as (.//*)[1].

I think I would use

<xsl:key name="p" match="*">
<xsl:number level="any" count="*" from="/"/>
</xsl:key>

then

<xsl:variable name="posn">
  <xsl:number level="any" count="*" from="/"/>
</xsl:variable>

<xsl:variable name="val" select="document($src)/key('p', $posn)"/>


Michael Kay Saxonica


On 30/07/2010 09:44, Dave Pawson wrote:
I've been working with a series of xhtml like files, cleaning them up.
It was a case of run an identity tranform, apply corrections.
That was the theory.
Luckily I kept a backup.
I hastily created an incorrect version of the identity transform
which omitted to copy the contents of any attributes.

I'm left with the problem of how to correct it.
Given
directory A contains the files to be corrected.
directory B contains the backup set of files.

I want to match on
"x:a[not(string(@href))]" I.e. those a elements with an empty href
attribute.

then copy across the value of the 'matching' href in the B directory,
same file. I'm iterating through the files in A using

  <xsl:for-each select="for $f in collection('/A?select=*.xml')
                          return $f">
  <xsl:result-document
        href="{replace(base-uri(),'A','AA')}">
        <xsl:apply-templates/>
    </xsl:result-document>
which creates the temporary directory AA with the corrections applied.


the problem I'm finding is picking up the correct a element in the second file?

Having found an empty a element

I'm trying
  <xsl:variable name="backup" select="'file:///B'"/>

   <xsl:template match="x:a[not(@href)]">
     <xsl:variable name="thisdoc" select="document-uri(/)"/>
     <xsl:variable name="base"
select="substring-after($thisdoc,'blg/')"/>
     <xsl:variable name="src" select="concat($backup,'/',$base)"
as="xs:string"/>   <!- this is the basename of the filename being worked
->

<-this is an attempt to select the 'right' a element->
     <xsl:variable name="posn" select="count(ancestor::*|preceding::*)"/>
     <xsl:variable name="val"
       select="(document($src)//*)[$posn]/@href"/>


<!-- Now copy this across --> <x:a href="{$val}"> <xsl:value-of select="."/> </x:a> </xsl:template>


Can anyone spot any obvious sillies please?



regards ------------------------ Dave Pawson http://www.dpawson.co.uk XSLT, XSL-FO and docbook FAQ

Current Thread