Returning empty tag if encountered white spaces(and other characters) other that specified delimiters using xslt when input is text file

StackOverflow https://stackoverflow.com/questions/21900246

  •  14-10-2022
  •  | 
  •  

Question

Suppose I have following text file

<sample>23=1111112345|40|0|0|1|0|0|0| =0|0||X,XXX,621</sample> 

Now I have to tarnsform this text document to xml through XSLT.Following is my xslt which I am using

<?xml version="1.1" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:msxml="urn:schemas-microsoft-com:xslt" version="1.0" 
xmlns:str="http://exslt.org/strings" exclude-result-prefixes="msxml">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <xsl:variable name="tokens" select="str:tokenize(Sample, '=|')" />
    <ResponseType>  
 <MId><xsl:value-of select="$tokens[1]" /></MId>
<SCode><xsl:value-of select="$tokens[2]" /></SCode>
<ANumber><xsl:value-of select="$tokens[3]" /></ANumber>
<Id><xsl:value-of select="$tokens[4]" /></Id>
<CtCode><xsl:value-of select="$tokens[5]" /></CtCode>
<TCode><xsl:value-of select="$tokens[6]" /></TCode>
 <ACode><xsl:value-of select="$tokens[7]" /></ACode>
 <RegeCode><xsl:value-of select="$tokens[8]" /></RegeCode>
<CHECKCODE><xsl:value-of select="$tokens[9]" /></CHECKCODE>
<TRANSFERCODE><xsl:value-of select="$tokens[10]" /></TRANSFERCODE>
<CODE><xsl:value-of select="$tokens[11]" /></CODE>
<ACTIONCODE><xsl:value-of select="$tokens[12]"/></ACTIONCODE>
<SoftCODE><xsl:value-of select="$tokens[13]"/></SoftCODE>
<ACCODE><xsl:value-of select="$tokens[14]"/></ACCODE>
 </ResponseType>
  </xsl:template>
</xsl:stylesheet>

Now my question is how do I get emty tags for the tags where there are white spaces--ie I want this output

<ResponseType>  
 <MId>23</MId>
<SCode>1111112345</SCode>
<ANumber>40</ANumber>
<Id>0</Id>
<CtCode>0</CtCode>
<TCode>1</TCode>
 <ACode>0</ACode>
 <RegeCode>0</RegeCode>
<CHECKCODE>0</CHECKCODE>
<TRANSFERCODE/>
<CODE>0</CODE>
<ACTIONCODE>0</ACTIONCODE>
<SoftCODE/>
<ACCODE>X,XXX,621</ACCODE>
 </ResponseType>
  </xsl:template>
</xsl:stylesheet>

Which condition I have to write so that I can get desired result

Thanks

Was it helpful?

Solution

how do I get emty tags for the tags where there are white spaces-

If you mean you want to eliminate white-space-only content in the result, use the normalize-space() function - for example:

<TRANSFERCODE><xsl:value-of select="normalize-space($tokens[10])" /></TRANSFERCODE>

Note that XML is case-sensitive: Sample is not the same as sample.

--

P.S. Please close you previous question: Str:Tokenize in XSLT not giving expected result

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top