XSLT transformation with role if find the same tag namepf

51 Views Asked by At

I'm working on an XSLT transformation and need assistance in achieving a specific transformation logic. Below are the details:

Objective: If the <namepf> tag is the same for different <record> elements, I want to concatenate the errors within the <errors> tag.

Input XML

<root>
    <record msg="File with error." status="ERROR" task="AD_CLT - Hiring/CLT">
        <client>4039</client>
        <company>84046101038328</company>
        <indcnp>1</indcnp>
        <namepf>Peter Santos</namepf>
        <ticket>A4039</ticket>
        <action>Save</action>
        <task>AD_CLT</task>
        <datenv error="Invalid registration." status="ERROR">01/11/2023</datenv>
        <horenv>09:15</horenv>
        <usuenv>34950</usuenv>
        <usubpo error="Invalid registration. 123" status="ERROR">Blenn-WEBSERVICE</usubpo>
        <indide>2</indide>
        <nreqid>HI0111091515290</nreqid>
        <idempr>01062748565</idempr>
        <idglob>69733</idglob>
        <matric error="Invalid registration. Next available number 2569999. ticket: 123 by employee 2569733." status="ERROR">2569733</matric>
        <tpRJor>1</tpRJor>
        <dataso>23/10/2023</dataso>
        <nromed>7749</nromed>
        <ufmed>GO</ufmed>
    </record>
    <record msg="File with error." status="ERROR" task="AD_CLT - Hiring/CLT">
        <client>4039</client>
        <company>84046101038328</company>
        <indcnp>1</indcnp>
        <ticket>A4039</ticket>
        <action>Save</action>
        <namepf>Peter Santos</namepf>
        <horenv>09:15</horenv>
        <usuenv>34950</usuenv>
        <usubpo error="Invalid registration. 123" status="ERROR">Blenn-WEBSERVICE</usubpo>
        <indide>2</indide>
        <nreqid>HI0111091515290</nreqid>
        <idempr error="Invalid registration. 456" status="ERROR">01062748565</idempr>
        <idglob error="Invalid registration. 789" status="ERROR">69733</idglob>
        <tpRJor>1</tpRJor>
        <dataso>23/10/2023</dataso>
        <nromed>7749</nromed>
        <ufmed>GO</ufmed>
    </record>
</root>

Output XML desired:

 <emailTitle><![CDATA[<record msg="File with error." status="ERROR" task="AD_CLT - Hiring/CLT">]]></emailTitle>
    <name>namepf: Peter Santos</name>
    <errors>
        <datenv error="Invalid registration." status="ERROR">01/11/2023</datenv>
        <usubpo error="Invalid registration. 123" status="ERROR">Blenn-WEBSERVICE</usubpo>
        <matric error="Invalid registration. Next available number 2569999. ticket: 123 by employee 2569733." status="ERROR">2569733</matric>
        <task error="Invalid registration. 123" status="ERROR">AD_CLT</task>
        <idempr error="Invalid registration. 456" status="ERROR">01062748565</idempr>
        <idglob error="Invalid registration. 789" status="ERROR">69733</idglob>
    </errors>
</root>

I've started with a basic XSLT, which is useful for copying elements with a status attribute of 'ERROR'. However, suggestions or modifications are needed to handle the concatenation of errors for records with the same <namepf>.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <!-- Match the root element -->
  <xsl:template match="root">
    <xsl:copy>
      <!-- Apply templates to child elements -->
      <xsl:apply-templates select="record[@status='ERRO']"/>
    </xsl:copy>
  </xsl:template>

  <!-- Match the 'record' element -->
  <xsl:template match="record">
    <xsl:copy>
      <!-- Copy 'record' attributes -->
      <xsl:copy-of select="@*"/>
      <!-- Apply templates to child elements with error status -->
      <xsl:apply-templates select="*[@status='ERRO']"/>
    </xsl:copy>
  </xsl:template>

  <!-- Match elements with error status -->
  <xsl:template match="*[not(self::record) and @status='ERRO']">
    <xsl:copy>
      <!-- Copy element attributes -->
      <xsl:copy-of select="@*"/>
      <!-- Copy element text content -->
      <xsl:copy-of select="text()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
0

There are 0 best solutions below