I would like to add an increment to an XML element by counting the number of nodes in the source file. This increment is placed in a particular condition that counts the position in a tree structure.
Here’s an example of the source file:
<ArchiveTransfer>
<DataObjectPackage>
<DescriptiveMetadata>
<ArchiveUnit id="ID20230407121050277">
<Content>
<Title>Information</Title>
</Content>
<ArchiveUnit id="ID20230407121050344">
<Content>
<Title>2212 Blogs</Title>
</Content>
<ArchiveUnit id="ID20230407121054394">
<Content>
<Title>Export eSPRIT</Title>
</Content>
<ArchiveUnit id="ID20230407121054576">
<Content>
<Title>eSPRIT</Title>
</Content>
</ArchiveUnit>
</ArchiveUnit>
</ArchiveUnit>
<ArchiveUnit id="ID20230407121054610">
<Content>
<Title>2212 Newsletter</Title>
</Content>
<ArchiveUnit id="ID20230407121054710">
<Content>
<Title>Entreprises</Title>
</Content>
</ArchiveUnit>
</ArchiveUnit>
</ArchiveUnit>
</DescriptiveMetadata>
</DataObjectPackage>
</ArchiveTransfer>
Je voudrais obtenir le résultat suivant :
<ead>
<archdesc>
<did>
<unittitle>Information</unittitle>
</did>
<dsc>
<c>
<did>
<unitid>1</unitid>
<unittitle>2212 Blogs</unittitle>
</did>
<c>
<did>
<unittitle>Export eSPRIT</unittitle>
</did>
<c>
<did>
<unittitle>eSPRIT</unittitle>
</did>
</c>
</c>
</c>
<c>
<did>
<unitid>2</unitid>
<unittitle>2212Newsletter </unittitle>
</did>
<test>1</test>
<c>
<did>
<unittitle>Entreprises</unittitle>
</did>
</c>
</c>
</dsc>
</archdesc>
</ead>
Voici ma feuille XSLT :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" encoding="utf-8"/>
<xsl:variable name="unitidCounter" select="1"/>
<xsl:template match="/">
<xsl:apply-templates
select="//ArchiveTransfer/DataObjectPackage/DescriptiveMetadata/ArchiveUnit"/>
</xsl:template>
<xsl:template match="//Archive/DescriptiveMetadata/ArchiveUnit">
<ead>
<archdesc>
<xsl:call-template name="did"/>
<dsc>
<xsl:apply-templates select="ArchiveUnit"/>
</dsc>
</archdesc>
</ead>
</xsl:template>
<xsl:template match="ArchiveUnit">
<c>
<xsl:call-template name="did"/>
<xsl:apply-templates select="ArchiveUnit">
<xsl:with-param name="unitidCounter" select="$unitidCounter + 1"/>
</xsl:apply-templates>
</c>
</xsl:template>
<xsl:template name="did">
<did>
<xsl:if test="count(ancestor::ArchiveUnit) = 1">
<unitid>
<xsl:value-of select="$unitidCounter"/>
</unitid>
</xsl:if>
</did>
</xsl:template>
<xsl:template match="Content/Title">
<unittitle type="titre" label="Intitulé de l'unité documentaire">
<xsl:apply-templates select="@language"/>
<xsl:apply-templates/>
</unittitle>
</xsl:template>
</xsl:stylesheet>
I try to increment the unitidCounter variable but the result I get is always 1. I think I’m incrementing this variable wrongly, but I can’t find my error. Any ideas? Thanks a lot!
Looks similar to stackoverflow.com/q/3344965/407651
Please explain the exact logic you are trying to implement. It seems you have a recursive structure of
ArchiveUnit
containing otherArchiveUnit
, but you only want to number the 2nd level??