Re: [xsl] new: how to change value of attributes

Subject: Re: [xsl] new: how to change value of attributes
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Wed, 25 Nov 2009 18:14:52 +0100
ivanmacculi@xxxxxxxxx wrote:

Hi all, i'm a librarian with a problem: i've to change the value of attribute for each element of a group that i'm not able to select and count.
i've something like this:

<mets:div LABEL="Testo" ORDER="4" ORDERLABEL="1">
<mets:div LABEL="Frontespizio" ORDER="1" ORDERLABEL="1">
<mets:div LABEL="Pagina 5" ORDER="1" ORDERLABEL="5">
<mets:fptr FILEID="file.00008"/>
</mets:div>
</mets:div>
<mets:div LABEL="Dedica" ORDER="2" ORDERLABEL="1">
<mets:div LABEL="Pagina 6" ORDER="1" ORDERLABEL="6">
<mets:fptr FILEID="file.00009"/>
</mets:div>
</mets:div>
<mets:div LABEL="Canto I" ORDER="3" ORDERLABEL="1">
<mets:div LABEL="Pagina 7" ORDER="1" ORDERLABEL="7">
<mets:fptr FILEID="file.00010"/>
</mets:div>
<mets:div LABEL="Pagina 8" ORDER="2" ORDERLABEL="8">
<mets:fptr FILEID="file.00011"/>
</mets:div>
<mets:div LABEL="Pagina 9" ORDER="3" ORDERLABEL="9">
<mets:fptr FILEID="file.00012"/>
</mets:div>
</mets:div>
</mets:div>

i have to change the value of LABEL from "pagina 5, pagina 6, pagina 7, pagina 8, etc..." to "carta 1r, carta 1v, carta 2r, carta 2v, etc".
So the problem is that i've a order of pages with a kind of enumeration, and it will became a list of a double enumeration. each number is repeated.
It will be something like this:

<mets:div LABEL="Testo" ORDER="4" ORDERLABEL="1">
<mets:div LABEL="Frontespizio" ORDER="1" ORDERLABEL="1">
<mets:div LABEL="Carta 1r" ORDER="1" ORDERLABEL="5">
<mets:fptr FILEID="file.00008"/>
</mets:div>
</mets:div>
<mets:div LABEL="Dedica" ORDER="2" ORDERLABEL="1">
<mets:div LABEL="Carta 1v" ORDER="1" ORDERLABEL="6">
<mets:fptr FILEID="file.00009"/>
</mets:div>
</mets:div>
<mets:div LABEL="Canto I" ORDER="3" ORDERLABEL="1">
<mets:div LABEL="Carta 2r" ORDER="1" ORDERLABEL="7">
<mets:fptr FILEID="file.00010"/>
</mets:div>
<mets:div LABEL="Carta 2v" ORDER="2" ORDERLABEL="8">
<mets:fptr FILEID="file.00011"/>
</mets:div>
<mets:div LABEL="Carta 3r" ORDER="3" ORDERLABEL="9">
<mets:fptr FILEID="file.00012"/>
</mets:div>
</mets:div>
</mets:div>

The following stylesheet produces above output (minus indentation):


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:mets="http://www.loc.gov/METS/";
  version="2.0">

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

<xsl:template match="mets:div/@LABEL[matches(., '^Pagina [0-9]+$')]">
<xsl:variable name="n">
<xsl:number level="any" count="mets:div[@LABEL[matches(., '^Pagina [0-9]+$')]]"/>
</xsl:variable>
<xsl:attribute name="LABEL" select="concat('Carta ', if ($n mod 2 = 0) then concat(($n + 1) idiv 2, 'v') else concat(($n + 1) idiv 2, 'r'))"/>
</xsl:template>


</xsl:stylesheet>

--

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread