lhovhannisyan
lhovhannisyan

Reputation: 25

Transforming XML to TXT using xslt

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>&#xA;</xsl:text>
    <xsl:text>KSIĄŻKA:&#xA;</xsl:text>
    <xsl:text>TYTUŁ:&#x9;</xsl:text>
    <xsl:value-of select="concat('&#x9;',tytuł)" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:text>AUTOR: &#x9;</xsl:text>
    <xsl:value-of select="concat('&#x9;&#x9;',autor)" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:text>ROK WYDANIA: &#x9;</xsl:text>
    <xsl:value-of select="concat('&#x9;',rokWydania)" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:text>WYDAWNICTWO: &#x9;</xsl:text>
    <xsl:value-of select="concat('&#x9;',wydawnictwo)" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:text>GATUNEK: &#x9;</xsl:text>
    <xsl:value-of select="concat('&#x9;',gatunek)" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:text>LICZBA STRON: &#x9;</xsl:text>
    <xsl:value-of select="concat('&#x9;&#x9;',liczbaStron)" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:text>CENA: &#x9;</xsl:text>
    <xsl:value-of select="concat('&#x9;',cena)" />
    <xsl:text>&#xA;</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

Answers (1)

Daniel Haley
Daniel Haley

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>&#xA;</xsl:text>
    <xsl:text>KSIĄŻKA:&#xA;</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,'&#xA;')"/>
  </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

Related Questions