XML : Handling multiple Conditional Looping in XSLT

I have a sample message which has to be converted to different output structure using the XSLT.

Incoming message is

   <document>      <ObjectId>      <ID>1000909090</ID>      <dlex>          <attrGroupMany name="streetinfo">              <row>    <!-- Mandatory Loop -->                  <attr name="streetcode">AS_DRAINED</attr>                   <attrQualMany name="streetintake">             <!-- Optional Loop -->                      <value qual="en">dvif1</value>                      <value qual="nl">dvif2</value>                  </attrQualMany>                  <attr name="streettype">BY_MEASURE</attr>                  <attrQual name="streetbasis" qual="ONZ">5</attrQual>                  <attrQual name="streetsize" qual="EA">1</attrQual>                  <attrQualMany name="streetsizeDescription">    <!-- Optional Loop -->                      <value qual="en">sz1</value>                      <value qual="hi">sz2</value>                  </attrQualMany>                  <attrGroupMany name="streetDetails">                      <row>                                     <!-- Optional Loop -->                          <attr name="streetTypeCode">FAT</attr>                          <attr name="streetValueIntakePercent">25</attr>                          <attr name="streetPrecisionCode">APPROXIMATELY</attr>                          <attrQualMany name="streetContained">   <!-- Optional Loop -->                              <value qual="ONZ">2</value>                              <value qual="OZA">3</value>                          </attrQualMany>                      </row>                      <row>                          <attr name="streetTypeCode">FAMS</attr>                          <attr name="streetValueIntakePercent">999</attr>                          <attr name="streetPrecisionCode">EXACT</attr>                          <attrQualMany name="streetContained">                              <value qual="ONZ">4</value>                              <value qual="OZA">5</value>                          </attrQualMany>                      </row>                  </attrGroupMany>              </row>          </attrGroupMany>      </dlex>  </ObjectId>  </document>    

OutPut Message is

  <?xml version="1.0" encoding="UTF-8"?>  <CatalogObjectId>  <RelationshipData>    <Relationship>       <RelationType>ObjectId_Street</RelationType>       <RelatedObjectIds>          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz1-en-FAT-2-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz1-en-FAT-3-OZA" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz1-en-FAMS-4-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz1-en-FAMS-5-OZA" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz2-hi-FAT-2-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz2-hi-FAT-3-OZA" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz2-hi-FAMS-4-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz2-hi-FAMS-5-OZA" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz1-en-FAT-2-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz1-en-FAT-3-OZA" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz1-en-FAMS-4-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz1-en-FAMS-5-OZA" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz2-hi-FAT-2-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz2-hi-FAT-3-OZA" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz2-hi-FAMS-4-ONZ" />          <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz2-hi-FAMS-5-OZA" />       </RelatedObjectIds>    </Relationship>  </RelationshipData>  </CatalogObjectId>    

This is working perfectly when we are using the below XSLT.

  <?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:template match="document">      <CatalogObjectId>          <RelationshipData>              <Relationship>                  <RelationType>ObjectId_Street</RelationType>                  <RelatedObjectIds>                      <xsl:for-each select="ObjectId/dlex/attrGroupMany[@name='streetinfo']/row">                            <xsl:variable name="v_position_streetinfo" select="position()" />                                                                             <xsl:variable name="v_streetcode">                              <xsl:value-of select="attr[@name='streetcode'])"/>                          </xsl:variable>                                           <xsl:variable name="v_streetintake" select="attrQualMany[@name = 'streetintake']/value" />                          <xsl:variable name="v_streetsizeDescription" select="attrQualMany[@name = 'streetsizeDescription']/value" />                          <xsl:variable name="v_streetDetails" select="attrGroupMany[@name = 'streetDetails']/row" />                                                   <xsl:for-each select="$v_streetintake">                              <xsl:variable name="v_streetintakevalue" select="." />                              <xsl:variable name="v_streetintakequal" select="./@qual" />                              <xsl:for-each select="$v_streetsizeDescription">                                  <xsl:variable name="v_streetsizeDescriptionvalue" select="." />                                  <xsl:variable name="v_streetsizeDescriptionqual" select="./@qual" />                                  <xsl:for-each select="$v_streetDetails">                                      <xsl:variable name="v_streetTypeCode">                                          <xsl:value-of select="attr[@name='streetTypeCode'])"/>                                      </xsl:variable>                                       <xsl:variable name="v_streetContained" select="attrQualMany[@name = 'streetContained']/value" />                                      <xsl:for-each select="$v_streetContained">                                          <xsl:variable name="v_streetContainedvalue" select="." />                                          <xsl:variable name="v_streetContainedqual" select="./@qual" />                                          <RelatedObjectId>                                              <xsl:attribute name="referenceKey">                                                                                                                        <xsl:value-of select="concat('ObjectId_Street','-',$v_position_streetinfo,'-',$v_streetcode,'-',$v_streetintakevalue,'-',$v_streetintakequal,'-',$v_streetsizeDescriptionvalue,'-',$v_streetsizeDescriptionqual,'-',$v_streetTypeCode,'-',$v_streetContainedvalue,'-',$v_streetContainedqual)"/>                                                                       </xsl:attribute>                                                          </RelatedObjectId>                                       </xsl:for-each>                                  </xsl:for-each>                              </xsl:for-each>                          </xsl:for-each>                      </xsl:for-each>                  </RelatedObjectIds>              </Relationship>           </RelationshipData>      </CatalogObjectId>  </xsl:template>  </xsl:stylesheet>    

But It doesn't work when anyone of the optional loops doesn't come. I have written the XSLT when all the optional loops are present, How can I write XSLT when any 1 or 2 or 3 or none of the optional groups comes in message. kindly Suggest

No comments:

Post a Comment