xslt-2.0xslt-3.0

How can I use XSLT to copy Parent Segment value to Child Segment?


i am trying to write XSLT mapping copy parent value to child nodes, ContainerCode -- > Serialshippingcontianercode value we need to pass to its subnodes under new tag , i have written XSLT but it is giving empty values for that segment, not giving any values for that segment.Please help me on this..

i attached sample input and output as below. Please check.

Input:

<StandardBusinessDocument>
  <receivingAdvice>
    <LogisticUnitLineItem>
      <logisticUnitIdentification>
        <ContainerCode>
          <serialShippingContainerCode>ContainerCode123</serialShippingContainerCode>
        </ContainerCode>
      </logisticUnitIdentification>
      <ContainmentLine number="1">
        <containedItemIdentification>
          <gtin>00000000000000</gtin>
          <Identification>
            <IdentificationValue>Value1</IdentificationValue>
            <IdentificationType>New1</IdentificationType>
          </Identification>
          <Identification>
            <IdentificationValue>Value2</IdentificationValue>
            <IdentificationType>New2</IdentificationType>
          </Identification>
        </containedItemIdentification>
      </ContainmentLine>
      <ContainmentLine number="2">
        <containedItemIdentification>
          <gtin>00000000000000</gtin>
          <Identification>
            <IdentificationValue>Value1</IdentificationValue>
            <IdentificationType>New1</IdentificationType>
          </Identification>
          <Identification>
            <IdentificationValue>Value2</IdentificationValue>
            <IdentificationType>New2</IdentificationType>
          </Identification>
        </containedItemIdentification>
      </ContainmentLine>
    </LogisticUnitLineItem>
    <LogisticUnitLineItem>
      <logisticUnitIdentification>
        <ContainerCode>
          <serialShippingContainerCode>ContainerCode2nd</serialShippingContainerCode>
        </ContainerCode>
      </logisticUnitIdentification>
      <ContainmentLine number="1">
        <containedItemIdentification>
          <gtin>00000000000000</gtin>
          <Identification>
            <IdentificationValue>686</IdentificationValue>
            <IdentificationType>SUPP</IdentificationType>
          </Identification>
          <Identification>
            <IdentificationValue>789</IdentificationValue>
            <IdentificationType>SUPP1</IdentificationType>
          </Identification>
        </containedItemIdentification>
      </ContainmentLine>
    </LogisticUnitLineItem>
  </receivingAdvice>
</StandardBusinessDocument>

** Desired Output:**

<StandardBusinessDocument>
   <receivingAdvice>
      <LogisticUnitLineItem>
         <logisticUnitIdentification>
            <ContainerCode>
               <serialShippingContainerCode>ContainerCode123</serialShippingContainerCode>
            </ContainerCode>
         </logisticUnitIdentification>
         <ContainmentLine number="1">
            <containedItemIdentification>
               <gtin>00000000000000</gtin>
               <Identification>
                  <IdentificationValue>Value1</IdentificationValue>
                  <IdentificationType>New1</IdentificationType>
               </Identification>
               <Identification>
                  <IdentificationValue>Value2</IdentificationValue>
                  <IdentificationType>New2</IdentificationType>
               </Identification>
               <Identification>
                  <IdentificationValue>ContainerCode123</IdentificationValue>
                  <IdentificationType>PAL</IdentificationType>
               </Identification>
            </containedItemIdentification>
         </ContainmentLine>
         <ContainmentLine number="2">
            <containedItemIdentification>
               <gtin>00000000000000</gtin>
               <Identification>
                  <IdentificationValue>Value1</IdentificationValue>
                  <IdentificationType>New1</IdentificationType>
               </Identification>
               <Identification>
                  <IdentificationValue>Value2</IdentificationValue>
                  <IdentificationType>New2</IdentificationType>
               </Identification>
               <Identification>
                  <IdentificationValue>ContainerCode123</IdentificationValue>
                  <IdentificationType>PAL</IdentificationType>
               </Identification>
            </containedItemIdentification>
         </ContainmentLine>
      </LogisticUnitLineItem>
      <LogisticUnitLineItem>
         <logisticUnitIdentification>
            <ContainerCode>
               <serialShippingContainerCode>ContainerCode2nd</serialShippingContainerCode>
            </ContainerCode>
         </logisticUnitIdentification>
         <ContainmentLine number="1">
            <containedItemIdentification>
               <gtin>00000000000000</gtin>
               <Identification>
                  <IdentificationValue>686</IdentificationValue>
                  <IdentificationType>SUPP</IdentificationType>
               </Identification>
               <Identification>
                  <IdentificationValue>789</IdentificationValue>
                  <IdentificationType>SUPP1</IdentificationType>
               </Identification>
               <Identification>
                  <IdentificationValue>ContainerCode2nd</IdentificationValue>
                  <IdentificationType>PAL</IdentificationType>
               </Identification>
            </containedItemIdentification>
         </ContainmentLine>
      </LogisticUnitLineItem>
   </receivingAdvice>
</StandardBusinessDocument>

** XSLT I used is below:**


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="LogisticUnitLineItem">
    <xsl:copy>
      <xsl:apply-templates select="ContainmentLine"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="LogisticUnitLineItem">
    <xsl:copy>
      <Identification>
        <IdentificationValue>
          <xsl:value-of select="/serialShippingContainerCode"/>
        </IdentificationValue>
        <IdentificationType>PAL</IdentificationType>
        <xsl:apply-templates select="Identification"/>
      </Identification>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Please assist here.


Solution

  • You could use this:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>  
      
      <xsl:template match="LogisticUnitLineItem">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()">
            <!-- Use a tunnel parameter to forward logisticUnitIdentification/ContainerCode/serialShippingContainerCode/text() -->
            <xsl:with-param name="serialShippingContainerCode" select="logisticUnitIdentification/ContainerCode/serialShippingContainerCode/text()" tunnel="yes"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template> 
      
      <!-- We only need to insert een extra Identification once; therefore added a predicate[last()]-->
      <xsl:template match="Identification[last()]">
        <xsl:param name="serialShippingContainerCode" tunnel="yes"/>
        <!-- Copy just the current node -->
        <xsl:copy-of select="."/>
        <xsl:copy>
          <IdentificationValue>
            <xsl:value-of select="$serialShippingContainerCode"/>
          </IdentificationValue>
          <IdentificationType>PAL</IdentificationType>
        </xsl:copy>
      </xsl:template> 
      
    </xsl:stylesheet>