Re: [xsl] Correcting a prior error

Subject: Re: [xsl] Correcting a prior error
From: Dave Pawson <davep@xxxxxxxxxxxxx>
Date: Sat, 31 Jul 2010 10:40:57 +0100
finish up this thread.
Using Mike Kays code, this version has
input param fil  name of file to be 'corrected'
e.g. saxon -it:main restoreBK.xsl op.xml
Has backup files in $backup
files to be amended in .
produces output in some substitute directory in path of current
directory (see result-document)


Thanks for the help.
DaveP



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:d="http://www.dpawson.co.uk/ns#";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:xhtml="http://www.w3.org/1999/xhtml";
  xmlns:a ="http://www.w3.org/2005/Atom";
xmlns:dc="http://purl.org/dc/elements/1.1/";
  exclude-result-prefixes="xs d a dc"
  version="2.0">


<!-- Identity template for xslt 2.0 -->
  <xsl:import href="/sgml/xsl/clean2.xsl"/>

<!-- Input parameter, filename, no extention -->
  <xsl:param name='fil' select="'NONE'" as="xs:string"/>


  <d:doc xmlns:d="http://www.dpawson.co.uk/ns#";>
    <d:revhistory>
      <d:purpose>
        <d:para>This stylesheet works with a file and a backup to
        produce a corrected version, in a new directory</d:para>
      </d:purpose>
      <d:revision>
        <d:revnumber>1.0</d:revnumber>
        <d:date>2010-07-31T10:00:45Z</d:date>
        <d:authorinitials>DaveP</d:authorinitials>
        <d:revdescription>
          <d:para></d:para>
        </d:revdescription>
        <d:revremark></d:revremark>
      </d:revision>
    </d:revhistory>
  </d:doc>
  

  <xsl:output method="xml" indent="yes" encoding="utf-8"/>

<!-- Input document(s) -->
  <xsl:variable name='col' 
          select="concat('/sgml/xsl?select=',$fil,'.xml')"
as="xs:string"/>

  <xsl:template match="/" name='main'>
    <xsl:for-each select="for $f in collection($col)
			  return $f">
      <xsl:message>
  Checking doc:    <xsl:value-of select="document-uri(.)"/>
      </xsl:message>
<!-- Update this to substitute 'another' directory in current path -->
      <xsl:result-document 
	  href="{replace(base-uri(),'xsl','tmp')}">
	<xsl:apply-templates/>
      </xsl:result-document>
    </xsl:for-each>
  </xsl:template>
  

<!-- Directory in which backups are held -->
    <xsl:variable name="backup" select="'file:///sgml/bktest'"/>


<!-- All elements, accessed by postion -->
<xsl:key name="p" match="*">
  <xsl:number level="any" count="*" from="/"/>
</xsl:key>


<!-- Element of interest which requires correcting-->
 <!-- In this case xhtml:a/@href -->
  <xsl:template match="*:a[not(string(@href))]">
    <xsl:variable name="thisdoc" select="document-uri(/)"/>
<!-- Obtain basename, no paths e.g. file.ext from /dir/dir/dir/file.ext
--> <xsl:variable name="base"
select="substring-after($thisdoc,'xsl/')"/>

    <!-- Source of backups, built from backup source and base -->
    <xsl:variable name="src" select="concat($backup,'/',$base)"
    as="xs:string"/> <!-- was    <xsl:variable name='posn'
    select="count((., ancestor::*, preceding::*))"/> --> <xsl:variable
    name="posn"> <xsl:number level="any" count="*" from="/"/>
</xsl:variable>
    <xsl:variable name="val" 
      select="document($src)/key('p', $posn)/@href"/>
    
    <xsl:message terminate='no'>
      src is <xsl:value-of select="$src"/>
      base is <xsl:value-of select="$base"/>
      thisdoc is <xsl:value-of select="$thisdoc"/>
      posn is <xsl:value-of select="$posn"/>
      val is "<xsl:value-of select="$val"/>"

      Found empty a element in file <xsl:value-of
      select="/a:entry/a:id"/> </xsl:message>

    <!-- Now copy this across -->
    <!-- I.e. apply the correction from the backup -->
    <xsl:copy>
      <xsl:attribute name="href">
         <xsl:value-of select="$val"/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </xsl:copy>
 
  </xsl:template>
</xsl:stylesheet>



-- 

regards 

-- 
Dave Pawson
XSLT XSL-FO FAQ.
http://www.dpawson.co.uk

Current Thread