I need to generate a incremental sequence number like the one below. If you could see the sequence number attribute below, it doesn't have a proper order, in some places it occurs in descendant node and some places in different order. Is there any generic code to generate this sequence number.
Currently, I tried using the below ways but these didn't work. Any help would be appreciated.
Sample XML:
<act>
<ActAssociation ***SeqNum="1"***>
<InitialRptIndicator>Y</InitialRptIndicator>
</ActAssociation>
<Party ***SeqNum="2"***>
<ActPartyTypeCode>35</ActPartyTypeCode>
<PartyName ***SeqNum="3"***>
<PartyNameTypeCode>L</PartyNameTypeCode>
</PartyName>
</Party>
<Party SeqNum="4">
<PartyName SeqNum="5">
<fc2:PartyNameTypeCode>L</fc2:PartyNameTypeCode>
</PartyName>
</act>
If you add the attributes by hard coding them I think the easiest solution in XSLT 2 and later is to do that in a variable so that you have a temporary tree, and then you push that tree through a mode that uses <xsl:number count="*[@SeqNum]" level="any"/>
:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="doc1">
<root>
<foo SeqNum=""/>
<bar>
<foobar SeqNum=""/>
</bar>
<baz SeqNum="">
<whatever/>
</baz>
</root>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="$doc1/node()"/>
</xsl:template>
<xsl:template match="*/@SeqNum">
<xsl:attribute name="{name()}">
<xsl:number count="*[@SeqNum]" level="any"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
See https://xsltfiddle.liberty-development.net/eiZQaFu/2 , it is XSLT 3 but for XSLT 2 you simply need to replace the xsl:mode
by the identity transformation template .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.