2016-12-22 6 views
1

J'ai un problème avec la mise en forme du texte dans le fichier TXT après la transformation xslt. Ceci est un exemple de mon fichier XML.Transformer XML en TXT en utilisant xslt

<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> 

pour le transformer J'utilise ce modèle:

<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> 

Ma sortie dans le fichier txt est irrégulier comme étant attendu.

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 

Tout ce que je veux, c'est aling colonne de droite des données. J'ai essayé de le faire en utilisant des fonctions de concat et de sous-chaîne ou en ajoutant la variable $ spaceCount, mais je n'ai pas pu obtenir le résultat attendu.

Des conseils pour résoudre ce problème?

Répondre

1

est ici une option à l'aide substring() dans un modèle nommé ...

XML Entrée

<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> 

Sortie

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