Reputation: 29
For a file like this one (with potentially additional attributes):
<text>
...
<p>"<char key="Utterson">I</char> saw <char key="Hyde">Mr. Hyde</char> go in by the old dissecting-room door, <char key="Poole">Poole</char>," <char key="Utterson">he</char> said. "Is that right, when <char key="Jekyll">Dr. Jekyll</char> is from home?"</p>
<p>"Quite right, <char key="Utterson">Mr. Utterson, sir</char>," replied <char key="Poole">the servant</char>. "<char key="Hyde">Mr. Hyde</char> has a key."</p>
<p>"<char key="Jekyll">Your master</char> seems to repose a great deal of trust in <char key="Hyde">that young man</char>, <char key="Poole">Poole</char>," resumed <char key="Utterson">the other</char> musingly.</p>
<p>"Yes, <char key="Utterson">sir</char>, <char key="Jekyll">he</char> do indeed," said <char key="Poole">Poole</char>. "<char key="servants">We</char> have all orders to obey <char key="Hyde">him</char>."</p>
<p>"<char key="Utterson">I</char> do not think <char key="Utterson">I</char> ever met <char key="">Mr. Hyde</char>?" asked <char key="Utterson">Utterson</char>.</p>
...
I would like to generate a copy, adding a series of numbered ids for each char
, such as:
<text>
...
<p>"<char key="Utterson" id="Utterson-1">I</char> saw <char key="Hyde" id="Hyde-1">Mr. Hyde</char> go in by the old dissecting-room door, <char key="Poole" id="Poole-1">Poole</char>," <char key="Utterson" id="Utterson-2">he</char> said. "Is that right, when <char key="Jekyll" id="Jekyll-1">Dr. Jekyll</char> is from home?"</p>
<p>"Quite right, <char key="Utterson" id="Utterson-3">Mr. Utterson, sir</char>," replied <char key="Poole" id="Poole-2">the servant</char>. "<char key="Hyde" id="Hyde-2">Mr. Hyde</char> has a key."</p>
<p>"<char key="Jekyll" id="Jekyll-2">Your master</char> seems to repose a great deal of trust in <char key="Hyde" id="Hyde-3">that young man</char>, <char key="Poole" id="Poole-3">Poole</char>," resumed <char key="Utterson" id="Utterson-4">the other</char> musingly.</p>
<p>"Yes, <char key="Utterson" id="Utterson-5">sir</char>, <char key="Jekyll" id="Jekyll3-">he</char> do indeed," said <char key="Poole" id="Poole-4">Poole</char>. "<char key="servants" id="servants-1">We</char> have all orders to obey <char key="Hyde" id="Hyde-4">him</char>."</p>
<p>"<char key="Utterson" id="Utterson-6">I</char> do not think <char key="Utterson" id="Utterson-7">I</char> ever met <char key="" id="-5">Mr. Hyde</char>?" asked <char key="Utterson" id="Utterson-8">Utterson</char>.</p>
...
without losing any of the previous attributes, but using on the contrary the key attribute to number each id. Is such a thing possible? Thank you in advance.
Upvotes: 1
Views: 745
Reputation: 70598
You should start with the identity template here, at that copies across all existing nodes and attributes
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
Then you just need a template to match the char
element, where you can add on the new attribute. You could do this by using xsl:number
<xsl:attribute name="id">
<xsl:value-of select="@key" />
<xsl:text>-</xsl:text>
<xsl:variable name="key" select="@key" />
<xsl:number level="any" count="char[@key = $key]"></xsl:number>
</xsl:attribute>
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="char">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="@key" />
<xsl:text>-</xsl:text>
<xsl:variable name="key" select="@key" />
<xsl:number level="any" count="char[@key = $key]"></xsl:number>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2