Creating "index" attribute element and incrementing it from 0
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 1
- Joined: Tue Apr 05, 2022 7:16 pm
Creating "index" attribute element and incrementing it from 0
Hi, I have this input xml :
which I need to transform to this output xml :
So I need to add an attribute with name="index" and value="" will increment from 0. This new attribute should be created AFTER the <type>ABCDE</type> for 1st case, <type>PQRST</type> for 2nd case, <type>JKLMN</type> for 3rd case, and similarly for the future <type>...</type> attributes as well.
I need to use XSLT for the transformation. The current XSLT I have is this :
But I am not getting the desired output from this XSLT. Could someone kindly help me out with this?
Code: Select all
<root>
<type>randomtext</type>
<attribute name="ath value"/>
...
<attribute name="nth value"/>
<attribute name="initial">
<respondtag>
<tagtext name="valA">
<texts>
<type>ABCDE</type>
<attribute name="abc" value="def" />
</texts>
</tagtext>
<tagtext name="valB">
<texts>
<type>PQRST</type>
<attribute name="thj" value="trf" />
</texts>
</tagtext>
<tagtext name="valC">
<texts>
<type>JKLMN</type>
<attribute name="lop" value="pth" />
</texts>
</tagtext>
<tagtext name="...">
<texts>
<type>...</type>
<attribute name="type" value="..." />
</texts>
</tagtext>
</respondtag>
</attribute>
</root>
Code: Select all
<root>
<type>randomtext</type>
<attribute name="ath value"/>
...
<attribute name="nth value"/>
<attribute name="initial">
<respondtag>
<tagtext name="valA">
<texts>
<type>ABCDE</type>
<attribute name="abc" value="def" />
<attribute name="index" value="0" />
</texts>
</tagtext>
<tagtext name="valB">
<texts>
<type>PQRST</type>
<attribute name="thj" value="trf" />
<attribute name="index" value="1" />
</texts>
</tagtext>
<tagtext name="valC">
<texts>
<type>JKLMN</type>
<attribute name="lop" value="ptq" />
<attribute name="index" value="2" />
</texts>
</tagtext>
<tagtext name="...">
<texts>
<type>...</type>
<attribute name="type" value="..." />
<attribute name="index" value="..." />
</texts>
</tagtext>
</respondtag>
</attribute>
</root>
I need to use XSLT for the transformation. The current XSLT I have is this :
Code: Select all
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="texts">
<xsl:copy>
<xsl:copy-of select="*"/>
<attribute name="index" value="{position()-1}"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
-
- Posts: 9421
- Joined: Fri Jul 09, 2004 5:18 pm
Re: Creating "index" attribute element and incrementing it from 0
Hi,
Using the "position()" function makes sense when you are inside a xsl:for-each element, otherwise it will probably always return "1".
Maybe you can use the "preceding:::" xpath axis, something like count(preceding::texts)

Regards,
Radu
Using the "position()" function makes sense when you are inside a xsl:for-each element, otherwise it will probably always return "1".
Maybe you can use the "preceding:::" xpath axis, something like count(preceding::texts)
I do not want to help too much because it's probably your homework, right?preceding Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
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