Adding an id-attribute to element
Questions about XML that are not covered by the other forums should go here.
-
- Posts: 6
- Joined: Thu Dec 22, 2016 2:22 pm
Adding an id-attribute to element
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!
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!
-
- Posts: 9434
- Joined: Fri Jul 09, 2004 5:18 pm
Re: Adding an id-attribute to element
Hi,
You could try to create an XSLT stylesheet like this:
and create a transformation scenario in Oxygen which applies it over the XML document.
Regards,
Radu
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>
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
-
- Posts: 9434
- Joined: Fri Jul 09, 2004 5:18 pm
Re: Adding an id-attribute to element
Hi,
In the XSLT you can compute the @id attribute value like:
Regards,
Radu
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)"/>
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
-
- Posts: 2
- Joined: Sun Feb 25, 2018 6:39 pm
Re: Adding an id-attribute to element
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
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
-
- Posts: 9434
- Joined: Fri Jul 09, 2004 5:18 pm
Re: Adding an id-attribute to element
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:
and also when you output the modified XML element use "xsl:copy" so that the namespace from the input XML is preserved
like:
Regards,
Radu
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"
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>
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service