1

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.

  1. xsl:value-of select="preceding-sibling::@[SeqNum]/@n"
  2. SeqNum="{format-number(count(preceding@SeqNum)+1,'0000')}"

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>
5
  • Do the elements in the XML input have that SeqNum attribute already and you only need to fix the values? Or do you need to add that attribute? If so, to which elements exactly? Commented Aug 6, 2018 at 15:41
  • No, the XML input doesn't have the SeqNum attribute, I need to add this attribute in the output XML. The elements are not in defined order or not for any specified node. But it is possible to set the SeqNum attribute to the first element. My approach was to get the value of Previous SeqNum attribute of current node and increment by 1. Commented Aug 6, 2018 at 15:52
  • There needs to be some criteria to decide on which elements you want to add the attribute as in your shown sample you have added it to some elements like fc2:Party but not to others like fc2:Activity or fc2:FilingDateText. So how do you decide or want the XSLT program to decide where to add the attribute? Commented Aug 6, 2018 at 15:57
  • I agree, There is no particular order for the SeqNum to occur. I hard code the attribute SeqNum in the transform XSLT for the required nodes. Also, This is how the FINCEN expects Suspicious activity report to be filed. Commented Aug 6, 2018 at 16:00
  • Please, provide the wanted result. From the question in its current form it is difficult to guess what is wanted Commented Apr 20, 2019 at 18:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.