Reputation: 25
I've got a problem with text formatting in TXT file after xslt transformation. This is sample of my XML file.
<książka id="k3">
<tytuł> "Ogniem i Mieczem" </tytuł>
<autor> Henryk Sienkiewicz </autor>
<rokWydania> 1884 </rokWydania>
<wydawnictwo> Świat Książki </wydawnictwo>
<gatunek>Powieść</gatunek>
<liczbaStron> 758 </liczbaStron>
<cena>40.99 PLN</cena>
<waluta> PLN </waluta>
</książka>
To transform it I'm using this template:
<xsl:template match="książka">
<xsl:text>
</xsl:text>
<xsl:text>KSIĄŻKA:
</xsl:text>
<xsl:text>TYTUŁ:	</xsl:text>
<xsl:value-of select="concat('	',tytuł)" />
<xsl:text>
</xsl:text>
<xsl:text>AUTOR: 	</xsl:text>
<xsl:value-of select="concat('		',autor)" />
<xsl:text>
</xsl:text>
<xsl:text>ROK WYDANIA: 	</xsl:text>
<xsl:value-of select="concat('	',rokWydania)" />
<xsl:text>
</xsl:text>
<xsl:text>WYDAWNICTWO: 	</xsl:text>
<xsl:value-of select="concat('	',wydawnictwo)" />
<xsl:text>
</xsl:text>
<xsl:text>GATUNEK: 	</xsl:text>
<xsl:value-of select="concat('	',gatunek)" />
<xsl:text>
</xsl:text>
<xsl:text>LICZBA STRON: 	</xsl:text>
<xsl:value-of select="concat('		',liczbaStron)" />
<xsl:text>
</xsl:text>
<xsl:text>CENA: 	</xsl:text>
<xsl:value-of select="concat('	',cena)" />
<xsl:text>
</xsl:text>
My output in txt file is irregular as being expected.
KSIĄŻKA:
TYTUŁ: "Mały Książe"
AUTOR: Antoine de Saint-Exupéry
ROK WYDANIA: 1943
WYDAWNICTWO: Zielona Sowa
GATUNEK: Nowela
LICZBA STRON: 62
CENA: 4.99 USD
All I want is to aling right column of data. I tried to do it using concat and substring functions or adding variable $spaceCount, but couldn't get expected result.
Any tips on how to solve this?
Upvotes: 1
Views: 434
Reputation: 52858
Here's an option using substring()
in a named template...
XML Input
<książka id="k3">
<tytuł> "Ogniem i Mieczem" </tytuł>
<autor> Henryk Sienkiewicz </autor>
<rokWydania> 1884 </rokWydania>
<wydawnictwo> Świat Książki </wydawnictwo>
<gatunek>Powieść</gatunek>
<liczbaStron> 758 </liczbaStron>
<cena>40.99 PLN</cena>
<waluta> PLN </waluta>
</książka>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="książka">
<xsl:text>
</xsl:text>
<xsl:text>KSIĄŻKA:
</xsl:text>
<xsl:call-template name="pad">
<xsl:with-param name="label" select="'TYTUŁ:'"/>
<xsl:with-param name="value" select="normalize-space(tytuł)"/>
</xsl:call-template>
<xsl:call-template name="pad">
<xsl:with-param name="label" select="'AUTOR:'"/>
<xsl:with-param name="value" select="normalize-space(autor)"/>
</xsl:call-template>
<xsl:call-template name="pad">
<xsl:with-param name="label" select="'ROK WYDANIA:'"/>
<xsl:with-param name="value" select="normalize-space(rokWydania)"/>
</xsl:call-template>
<xsl:call-template name="pad">
<xsl:with-param name="label" select="'WYDAWNICTWO:'"/>
<xsl:with-param name="value" select="normalize-space(wydawnictwo)"/>
</xsl:call-template>
<xsl:call-template name="pad">
<xsl:with-param name="label" select="'GATUNEK:'"/>
<xsl:with-param name="value" select="normalize-space(gatunek)"/>
</xsl:call-template>
<xsl:call-template name="pad">
<xsl:with-param name="label" select="'LICZBA STRON:'"/>
<xsl:with-param name="value" select="normalize-space(liczbaStron)"/>
</xsl:call-template>
<xsl:call-template name="pad">
<xsl:with-param name="label" select="'CENA:'"/>
<xsl:with-param name="value" select="normalize-space(cena)"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="pad">
<xsl:param name="label"/>
<xsl:param name="value"/>
<xsl:variable name="padding"
select="' '"/>
<xsl:value-of select="concat($label,
substring($padding,string-length($label) + string-length($value)),
$value,'
')"/>
</xsl:template>
</xsl:stylesheet>
Output
KSIĄŻKA:
TYTUŁ: "Ogniem i Mieczem"
AUTOR: Henryk Sienkiewicz
ROK WYDANIA: 1884
WYDAWNICTWO: Świat Książki
GATUNEK: Powieść
LICZBA STRON: 758
CENA: 40.99 PLN
Upvotes: 1