Page 1 of 1

Adding an id-attribute to element

Posted: Wed Jul 12, 2017 11:53 am
by bee123
Hello,
I've got another newbie question.
How can I add an id-attribute with consecutive numbers in it in my xml.file.

It should look something like that:

<text id='1'>
<text id='2'>
<text id='3'>
<text id='4'>



Thank you!

Re: Adding an id-attribute to element

Posted: Wed Jul 12, 2017 12:33 pm
by Radu
Hi,

You could try to create an XSLT stylesheet like this:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">

<!-- Copy anything as it is -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="text">
<text>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id" select="count(preceding::text) + 1"/>
<xsl:apply-templates select="node()"/>
</text>
</xsl:template>
</xsl:stylesheet>
and create a transformation scenario in Oxygen which applies it over the XML document.

Regards,
Radu

Re: Adding an id-attribute to element

Posted: Wed Jul 12, 2017 2:26 pm
by bee123
Thank you! That works wonderfully!

What would I have to change to put a word in front of the numbers? The first id would look like id=abc01.

Thanks again

Re: Adding an id-attribute to element

Posted: Wed Jul 12, 2017 2:32 pm
by Radu
Hi,

In the XSLT you can compute the @id attribute value like:

Code: Select all

<xsl:attribute name="id" select="concat('abc', count(preceding::text) + 1)"/>
Regards,
Radu

Re: Adding an id-attribute to element

Posted: Sun Feb 25, 2018 8:08 pm
by RethLin
Hi!
I have to do the same inside the tag <p> into a TEI file, and I have tried to use the same code (changing the name of element), but the attribute doesn't added...
If I paste the code and apply to my xml file without any change, the new file xml has more attribute in my <div> element (part="N" org="uniform" sample="complete"), and the attribute @part=N appears also inside my <p> element: I cannot understand how is it possible.
Could someone explain me this weird event?

Thank you.
Lin

Re: Adding an id-attribute to element

Posted: Mon Feb 26, 2018 10:43 am
by Radu
Hi Lin,

TEI documents have the namespace "http://www.tei-c.org/ns/1.0" so on the "xsl:stylesheet" root element you should add this attribute:

Code: Select all

xpath-default-namespace="http://www.tei-c.org/ns/1.0"
and also when you output the modified XML element use "xsl:copy" so that the namespace from the input XML is preserved

like:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0" xpath-default-namespace="http://www.tei-c.org/ns/1.0">

<!-- Copy anything as it is -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="p">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id" select="count(preceding::text) + 1"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Regards,
Radu

Re: Adding an id-attribute to element

Posted: Tue Feb 27, 2018 1:00 am
by RethLin
Yes, I forgot the xpath in the root!
Thank you!
Lin