Home > OS >  Form multiple parent elements if condition is met- using xslt 1
Form multiple parent elements if condition is met- using xslt 1

Time:04-01

I have two scenarios:

  • Scenario 1: L node having child node SL
  • Scenario 2: L node with no child node SL

I need to form multiple L nodes if text "L1" (<L Id="L1">) is found at other nodes like <SL id="L1S1"> and <pit ref="L1S1">. Id attribute of SL node(i.e <SL id="L1S1">) is formed using "L1" in <L Id="L1">. Also ref attribute of pit node(i.e <pit ref="L1S1">) is formed using "L1" in <L Id="L1">, I need to check whether "L1" is present in either id attribute of SL or ref attribute of pit and form the desired output.

Input xml as below

<root>
  <L Id="L1">
    <test>ed</test>
    <SL id="L1S1">
      <check>
        <AId>1</AId>
      </check>
      <MD>
        <UnitNumber>1</UnitNumber>
      </MD>
    </SL>
<SL id="L1S2">
      <check>
        <AId>2</AId>
      </check>
      <MD>
        <UnitNumber>2</UnitNumber>
      </MD>
    </SL>
  </L>
  <cp>
    <current>
      <Amt>20154.00</Amt>
    </current>
    <pi>
      <pit ref="L1S1">
        <value>123</value>
      </pit>
<pit ref="L1S2">
        <value>1232</value>
      </pit>
    </pi>
  </cp>
</root>

Expected output should be:

<root>
  <L Id="L1">
    <SL id="L1S1">
      <check>
        <AId>1</AId>
      </check>
      <MD>
        <UnitNumber>1</UnitNumber>
      </MD>
    </SL>
    <pit ref="L1S1">
      <value>123</value>
    </pit>
  </L>
  <L Id="L1">
    <SL id="L1S2">
      <check>
        <AId>2</AId>
      </check>
      <MD>
        <UnitNumber>2</UnitNumber>
      </MD>
    </SL>
    <pit ref="L1S2">
      <value>1232</value>
    </pit>
  </L>
</root>
<root>
  <L Id="L1">
    <test>ed</test>    
  </L>
  <cp>
    <current>
      <Amt>20154.00</Amt>
    </current>
    <pi>
      <pit ref="L1S1">
        <value>123</value>
      </pit>
 <pit ref="L1S2">
        <value>1232</value>
      </pit>
    </pi>
  </cp>
</root>

Expected output should be:

<root>
  <L Id="L1">
    <pit ref="L1S1">
      <value>123</value>
    </pit>
  </L>
  <L Id="L1">
    <pit ref="L1S2">
      <value>1232</value>
    </pit>
  </L>
</root>

Can anyone help me with a solution which can work for both scenarios?

The above xslt is working for first scenario but fails for second input xml, may be I need to use contains() or something like that, I'm not sure of it.

I am using xslt1.0, and I have issue forming each L1 node for second scenario.

<xsl:key name="pit" match="pit" use="@ref" />
<xsl:copy>
  <xsl:for-each select="L/SL">
    <xsl:element name="{../@Id}">
      <xsl:copy-of select="."/>
      <xsl:copy-of select="key('pit', @Id)"/>
    </xsl:element>
  </xsl:for-each>
</xsl:copy>

CodePudding user response:

I suppose in the second scenario there is a relation between the L/@Id and the pit/@ref. For now I used the assumption that the first two chars of pit/@ref should match the L/@Id.

If that is correct you could try something like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
 >
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:key name="pit-SL" match="pit" use="@ref" />
  <xsl:key name="pit-L"  match="pit" use="substring(@ref,1,2)" />
  
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="L[SL]">
    <xsl:apply-templates select="SL"/>
  </xsl:template>
  
  <xsl:template match="SL">
    <L>
      <xsl:copy-of select="parent::L/@Id"/>
      <xsl:copy>
        <xsl:copy-of  select="@id"/>
        <xsl:apply-templates select="node()"/>
        <xsl:copy-of select="key('pit-SL',@id)"/>
      </xsl:copy>
    </L>
  </xsl:template>
  
  <xsl:template match="L[not(SL)]">
    <xsl:apply-templates select="key('pit-L',@Id)">
      <xsl:with-param name="L" select="."/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="pit">
    <xsl:param name="L"/>
    <L>
      <xsl:copy-of select="$L/@Id"/>
      <xsl:copy-of select="."/>
    </L>
  </xsl:template>

  <xsl:template match="cp"/>
    
</xsl:stylesheet>
  • Related