-1

Below is the xml

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ser="http://xyz.com.zr/l8q/12Q/service/">
    <soapenv:Header>
        <ser:User>
            <!-- comment -->
            <Username/>
            <password/>
        </ser:User>
    </soapenv:Header>
    <soapenv:Body>
        <mainTag>
            <abc>1596056</abc>
            <asd>12434F</asd>
            <def>wert</def>
            <childtag>
                <asdf>1233</asdf>
                <qwe>567</qwe>
            </childtag>
        </mainTag>
    </soapenv:Body>
</soapenv:Envelope>

Below is my XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

    <xsl:template match="mainTag">
        <xsl:result-document href="foo.txt" method="text">
            01|<xsl:value-of select="abc"/>|<xsl:value-of select="asd"/>|<xsl:value-of select="childtag/asdf"/>|
            02|<xsl:value-of select="def"/>|<xsl:value-of select="childtag/asdf"/>|
            03|<xsl:value-of select="def"/>|<xsl:value-of select="childtag/qwe"/>|<xsl:value-of select="childtag/asdf"/>| | 
        </xsl:result-document>
    </xsl:template>
</xsl:stylesheet>

Below is the output generated from above xslt enter image description here

Below is the text file which contains data from the xslt output enter image description here

i am stuck with how i can remove the spaces in the file (screen shot mentioned in the above screenshot)

user2587669
  • 532
  • 4
  • 10
  • 22

1 Answers1

0

One way to eliminate the white spaces from your output is to eliminate them from your stylesheet. Instead of:

    <xsl:template match="mainTag">
        <xsl:result-document href="foo.txt" method="text">
            01|<xsl:value-of select="abc"/>|<xsl:value-of select="asd"/>|<xsl:value-of select="childtag/asdf"/>|
            02|<xsl:value-of select="def"/>|<xsl:value-of select="childtag/asdf"/>|
            03|<xsl:value-of select="def"/>|<xsl:value-of select="childtag/qwe"/>|<xsl:value-of select="childtag/asdf"/>| | 
        </xsl:result-document>
    </xsl:template>

use:

<xsl:template match="mainTag">
<xsl:result-document href="foo.txt" method="text">01|<xsl:value-of select="abc"/>|<xsl:value-of select="asd"/>|<xsl:value-of select="childtag/asdf"/>|
02|<xsl:value-of select="def"/>|<xsl:value-of select="childtag/asdf"/>|
03|<xsl:value-of select="def"/>|<xsl:value-of select="childtag/qwe"/>|<xsl:value-of select="childtag/asdf"/>| |</xsl:result-document>
</xsl:template>

Note the lack of indent. This, together with:

<xsl:strip-space elements="*"/>

will produce the required result.

Of course, it's much more convenient to use xsl:text instructions to output literal text, e.g.

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="mainTag">
    <xsl:result-document href="foo.txt" method="text">
        <xsl:text>01|</xsl:text>
        <xsl:value-of select="abc"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="asd"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="childtag/asdf"/>
        <xsl:text>|&#10;02|</xsl:text>
        <xsl:value-of select="def"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="childtag/asdf"/>
        <xsl:text>|&#10;03|</xsl:text>
        <xsl:value-of select="def"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="childtag/qwe"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="childtag/asdf"/>
        <xsl:text>| |</xsl:text>
    </xsl:result-document>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Just a tip, sometimes you can use comments in xsl so you can keep format of code (new lines & indents) and it won't be at the output (as is code comment) – vegetable Mar 14 '23 at 10:47