1

below one is the XML input,

<?xml version="1.0" encoding="utf-8"?>
<GSK_Canonical_MESGX2>
 <header SEGMENT="1">
  <orderNumber>002001454979</orderNumber>
  <batchNumber>0000617944</batchNumber>
  <BOM SEGMENT="1">
   <operationNumber>0030</operationNumber>
   <phaseIndicator>0011</phaseIndicator>
  </BOM>
  <BOM SEGMENT="1">
   <operationNumber>0040</operationNumber>
   <phaseIndicator>0012</phaseIndicator>
  </BOM>
  <recipe SEGMENT="1">
   <phase>0011</phase>
   <parentOperation>0030</parentOperation>
   <workcenter>MANUOHD1</workcenter>
  </recipe>
  <recipe SEGMENT="1">
   <phase>0012</phase>
   <parentOperation>0040</parentOperation>
   <workcenter>COSTOHD1</workcenter>
  </recipe>
 </header>
</GSK_Canonical_MESGX2>

I have an below xslt,

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:set="http://exslt.org/sets" xmlns:str="http://exslt.org/strings" xmlns:java="http://xml.apache.org/xslt/java" xmlns:saxon="http://saxon.sf.net/" exclude-result-prefixes="exsl set str java saxon">
 <xsl:output method="text"/>
 <xsl:variable name="VarHash" select="'#'"/>
 <xsl:variable name="VarBreak" select="'&#xa;'"/>
 <xsl:variable name="pipeFieldDelimiter" select="'\|'"/>
 <xsl:template match="/">
  <xsl:text>HEADER</xsl:text>
  <xsl:value-of select="$VarHash"/>
  <xsl:value-of select="GSK_Canonical_MESGX2/header/orderNumber"/>
  <xsl:value-of select="$VarHash"/>
  <xsl:value-of select="GSK_Canonical_MESGX2/header/batchNumber"/>
  <xsl:value-of select="$VarBreak"/>
  <xsl:for-each select="GSK_Canonical_MESGX2/header/BOM">
   <!--GSK_Canonical_MESGX2/Header/BOM/OperationNumber = GSK_Canonical_MESGX2/header/recipe/parentOperation and GSK_Canonical_MESGX2/Header/BOM/phaseIndicator = GSK_Canonical_MESGX2/header/recipe/phase then  <xsl:value-of select="GSK_Canonical_MESGX2/header/recipe/workcenter"/> This needs to be implemented for each line item of BOM tag  -->
   <xsl:if test="position() != last()">
    <xsl:value-of select="$VarBreak"/>
   </xsl:if>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

below one is the expected output,

HEADER#002001454979#0000617944
MANUOHD1
COSTOHD1

now need to implement for each BOM line item,we need to compare BOM with Recipe tags and select workcenter value if the condition satisfied.

Header/BOM/OperationNumber = header/recipe/parentOperation 
  and 
Header/BOM/phaseIndicator = header/recipe/phase 
  then 
<xsl:value-of select="GSK_Canonical_MESGX2/header/recipe/workcenter"/>

Please help me to achieve this.Thanks

1 Answers1

0

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:key name="kBomByPhaseAndOperation" match="BOM"
  use="concat(operationNumber, '|', phaseIndicator)"/>

  <xsl:template match=
   "recipe[key('kBomByPhaseAndOperation',
               concat(parentOperation, '|', phase))
           ]">
    <xsl:value-of select="concat('&#xA;', workcenter)"/>
  </xsl:template>

  <xsl:template match="header">
    <xsl:text>HEADER</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="orderNumber|batchNumber">
    <xsl:value-of select="concat('#', .)"/>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>

When applied on the provided source XML document:

<GSK_Canonical_MESGX2>
    <header SEGMENT="1">
        <orderNumber>002001454979</orderNumber>
        <batchNumber>0000617944</batchNumber>
        <BOM SEGMENT="1">
            <operationNumber>0030</operationNumber>
            <phaseIndicator>0011</phaseIndicator>
        </BOM>
        <BOM SEGMENT="1">
            <operationNumber>0040</operationNumber>
            <phaseIndicator>0012</phaseIndicator>
        </BOM>
        <recipe SEGMENT="1">
            <phase>0011</phase>
            <parentOperation>0030</parentOperation>
            <workcenter>MANUOHD1</workcenter>
        </recipe>
        <recipe SEGMENT="1">
            <phase>0012</phase>
            <parentOperation>0040</parentOperation>
            <workcenter>COSTOHD1</workcenter>
        </recipe>
    </header>
</GSK_Canonical_MESGX2>

produces exactly the wanted, correct result:

HEADER#002001454979#0000617944
MANUOHD1
COSTOHD1
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431