0

The Detailed specification of the requirement is, in the Request message there is a field by name IgnoreEmptyElementInd it is of boolean type. If that field is True, need to ignore the empty element fields in the request message and default values like "-111" shd be set as default value found. In Case of false, no need to delete empty elements only if default values are found it shd be notified.

My Input message is

<SampleUpdRq>
     <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
     <UpdMsgRqHdr>
          <ContextRqHdr>
              <RsSelURL>111</RsSelURL>
              <NetworkTrnData>
                    <TerminalIdent>a</TerminalIdent>
                    <Name>111</Name>
              </NetworkTrnData>
              <ClientApp>
                     <Org>dweer</Org>
                     <Name>aaaaaaaaaaaaaaaaaaa</Name>
                     <Version>112</Version>
                     <Channel>abc</Channel>
              </ClientApp>
          </ContextRqHdr>
          <IgnoreEmptyElementInd>true</IgnoreEmptyElementInd>
     </UpdMsgRqHdr>
     <SampleKeys>
           <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
           <AltSampleIdentifiers>
              <SampleIdent>
                   <SampleIdentType>-111</SampleIdentType>
                    <SampleIdentValue>ttttttt</SampleIdentValue>
              </SampleIdent>
              <SampleIdentType/>
           </AltSampleIdentifiers>
           <SampleType>
                <SampleTypeValue>MMA</SampleTypeValue>
                <SampleTypeSrc>bbc</SampleTypeSrc>
                <CommercialSampleType></CommercialSampleType>
           </SampleType>
           <CommercialSampleType>-111</CommercialSampleType>
           <COID>aaaaa</COID>
       </SampleKeys>
</SampleUpdRq> 

If the value of IgnoreEmptyElementInd is true, then output shd be like

<SampleUpdRq>
     <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
     <UpdMsgRqHdr>
          <ContextRqHdr>
              <RsSelURL>111</RsSelURL>
              <NetworkTrnData>
                    <TerminalIdent>a</TerminalIdent>
                    <Name>111</Name>
              </NetworkTrnData>
              <ClientApp>
                     <Org>dweer</Org>
                     <Name>aaaaaaaaaaaaaaaaaaa</Name>
                     <Version>112</Version>
                     <Channel>abc</Channel>
              </ClientApp>
          </ContextRqHdr>
          <IgnoreEmptyElementInd>true</IgnoreEmptyElementInd>
     </UpdMsgRqHdr>
     <SampleKeys>
           <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
           <AltSampleIdentifiers>
              <SampleIdent>
                   <SampleIdentType>Default Found</SampleIdentType>
                    <SampleIdentValue>ttttttt</SampleIdentValue>
              </SampleIdent>
           </AltSampleIdentifiers>
           <SampleType>
                <SampleTypeValue>MMA</SampleTypeValue>
                <SampleTypeSrc>bbc</SampleTypeSrc>
           </SampleType>
           <CommercialSampleType>Default Found</CommercialSampleType>
           <COID>aaaaa</COID>
       </SampleKeys>
</SampleUpdRq>

If the value of IgnoreEmptyElementInd is false, then output shd be like

<SampleUpdRq>
     <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
     <UpdMsgRqHdr>
          <ContextRqHdr>
              <RsSelURL>111</RsSelURL>
              <NetworkTrnData>
                    <TerminalIdent>a</TerminalIdent>
                    <Name>111</Name>
              </NetworkTrnData>
              <ClientApp>
                     <Org>dweer</Org>
                     <Name>aaaaaaaaaaaaaaaaaaa</Name>
                     <Version>112</Version>
                     <Channel>abc</Channel>
              </ClientApp>
          </ContextRqHdr>
          <IgnoreEmptyElementInd>false</IgnoreEmptyElementInd>
     </UpdMsgRqHdr>
     <SampleKeys>
           <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
           <AltSampleIdentifiers>
              <SampleIdent>
                   <SampleIdentType>Default Found</SampleIdentType>
                    <SampleIdentValue>ttttttt</SampleIdentValue>
              </SampleIdent>
              <SampleIdentType/>
           </AltSampleIdentifiers>
           <SampleType>
                <SampleTypeValue>MMA</SampleTypeValue>
                <SampleTypeSrc>bbc</SampleTypeSrc>
            <CommercialSampleType/>
           </SampleType>
           <CommercialSampleType>Default Found</CommercialSampleType>
           <COID>aaaaa</COID>
       </SampleKeys>
</SampleUpdRq>

I have written XSL like this :

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

<xsl:template match="*[not(node())]" />     

<xsl:template match="*[. = '-111']">       
  <xsl:copy>          
    <xsl:text>Default Found</xsl:text>
 </xsl:copy>    
</xsl:template>     

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

</xsl:stylesheet> 

Really appreciate the expertise help and sorry for the confusion. Thanks in advance.

8
  • 1
    Your first XSLT sample has version="2.0", do you use an XSLT 2.0 processor? And consider to post a representative XML input sample and the corresponding result you want to create, then we can suggest the right XSLT way of solving that. Commented Jan 2, 2012 at 17:58
  • You need to define the condition that is used to delete elements. Please, edit the question and add this. Commented Jan 2, 2012 at 18:18
  • @Vinay: Does IgnoreEmptyElementInd apply to the whole XML document, or just the UpdMsgRqHdr element? Commented Jan 4, 2012 at 8:48
  • @Tim C :IgnoreEmptyElementInd will apply to the whole XML document except the UpdMsgRqHdr element. IgnoreEmptyElementInd will be the last element of UpdMsgRqHdr, so for the elements coming after IgnoreEmptyElementInd, the rule should be applied. Commented Jan 4, 2012 at 9:24
  • @Tim C: Could this be done in XSL or not?? Commented Jan 4, 2012 at 16:44

1 Answer 1

1

In one of your comments you say "the elements coming after IgnoreEmptyElementInd, the rule should be applied", in which case it sounds you need to make use of the 'preceding' axis in XPath to determine the value.

So, you could extend your XSLT with this template to ignore empty nodes when IgnoreEmptyElementInd is set to true.

<xsl:template 
   match="*[not(node())][preceding::IgnoreEmptyElementInd[. = 'true']]"/>

So, given the following XSLT

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

   <xsl:template match="*[not(node())][preceding::IgnoreEmptyElementInd[. = 'true']]"/>

   <xsl:template match="*[. = '-111']">
      <xsl:copy>
         <xsl:text>Default Found</xsl:text>
      </xsl:copy>
   </xsl:template>

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

When applied to your sample XML, the following is output

<SampleUpdRq>
   <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
   <UpdMsgRqHdr>
      <ContextRqHdr>
         <RsSelURL>111</RsSelURL>
         <NetworkTrnData>
            <TerminalIdent>a</TerminalIdent>
            <Name>111</Name>
         </NetworkTrnData>
         <ClientApp>
            <Org>dweer</Org>
            <Name>aaaaaaaaaaaaaaaaaaa</Name>
            <Version>112</Version>
            <Channel>abc</Channel>
         </ClientApp>
      </ContextRqHdr>
      <IgnoreEmptyElementInd>true</IgnoreEmptyElementInd>
   </UpdMsgRqHdr>
   <SampleKeys>
      <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
      <AltSampleIdentifiers>
         <SampleIdent>
            <SampleIdentType>Default Found</SampleIdentType>
            <SampleIdentValue>ttttttt</SampleIdentValue>
         </SampleIdent>
      </AltSampleIdentifiers>
      <SampleType>
         <SampleTypeValue>MMA</SampleTypeValue>
         <SampleTypeSrc>bbc</SampleTypeSrc>
      </SampleType>
      <CommercialSampleType>Default Found</CommercialSampleType>
      <COID>aaaaa</COID>
   </SampleKeys>
</SampleUpdRq>

When IgnoreEmptyElementInd is set to 'false' the following is output instead

<SampleUpdRq>
   <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
   <UpdMsgRqHdr>
      <ContextRqHdr>
         <RsSelURL>111</RsSelURL>
         <NetworkTrnData>
            <TerminalIdent>a</TerminalIdent>
            <Name>111</Name>
         </NetworkTrnData>
         <ClientApp>
            <Org>dweer</Org>
            <Name>aaaaaaaaaaaaaaaaaaa</Name>
            <Version>112</Version>
            <Channel>abc</Channel>
         </ClientApp>
      </ContextRqHdr>
      <IgnoreEmptyElementInd>false</IgnoreEmptyElementInd>
   </UpdMsgRqHdr>
   <SampleKeys>
      <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
      <AltSampleIdentifiers>
         <SampleIdent>
            <SampleIdentType>Default Found</SampleIdentType>
            <SampleIdentValue>ttttttt</SampleIdentValue>
         </SampleIdent>
         <SampleIdentType/>
      </AltSampleIdentifiers>
      <SampleType>
         <SampleTypeValue>MMA</SampleTypeValue>
         <SampleTypeSrc>bbc</SampleTypeSrc>
         <CommercialSampleType/>
      </SampleType>
      <CommercialSampleType>Default Found</CommercialSampleType>
      <COID>aaaaa</COID>
   </SampleKeys>
</SampleUpdRq>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply @Tim C. I have 1 more doubt, in the above XML msg deris field called IgnoreEmptyElementInd dis is of boolean type. If the value is true of IgnoreEmptyElementInd =>Need to delete all the empty element fields in the message =>And there will be few defalut values like "1200-1-1" for Date type field and "-111" for integer type field, for elements having these values only element field shd be retained. If the value is false =>No need to delete the empty element fields in the message =>Need to delete the elements with default values like mentioned above.Is it possible in XSL?
You either need to amend the original question with the full and detailed requirements, so there is no misunderstanding. If you give the full rules for how "IgnoreEmptyElementInd" works, it should be possible to do.
Changed the original question with detailed Requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.