Project

Profile

Help

Problem to génération a text file

Added by Anonymous over 18 years ago

Legacy ID: #3826645 Legacy Poster: stepd (stepd)

Hi, I want to transform an XML file whith XSL into a flat file text. When i transform it whith XML Spy i've got no problem but when i transform it with saxon it generates some tabulations. Example : 01 2GBLV GP1F 112GBLV GP1F 0000005101720 01 2GBLV GT1 112GBLV GT1 0000004~ This is a part of my XSL file : <?xml version="1.0" encoding="UTF-8"?> <!--Feuille XSL qui convertit les accusés de réception des commandes (ordres de livraison - fichiers .PKT) de PKMS vers un fichier à plat au format Roederer--> <!--Auteur : SD (17/07/2006)--> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"> <xsl:import href="communs.xsl"/> <xsl:output method="text" media-type="text/plain" indent="no"/> <xsl:template match="/ListOfPKT/PKT"> <!--création de la variable cpt pour connaitre la position du PKT que l'on est en train de traiter--> <xsl:variable name="cpt"> <xsl:number count="//PKT"/> </xsl:variable> <!--test si le ManagingTerritory = TID ou LIF ou LIV car sinon on ne traite pas ce PKT--> <xsl:if test="/ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='TID' or /ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='LIF' or /ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='LIV'"> <!--traitement du 1er type d'enregistrement-->01<!--appel du template WriteData qui met à jour les champs du fichier cible--> <xsl:call-template name="WriteData"> <!--On lui passe la variable cpt (pour la même raison que plus haut)--> <xsl:with-param name="cptPara"> <xsl:value-of select="$cpt"/> </xsl:with-param> </xsl:call-template> <!--traitement du 2ème type d'enregistrement-->&#10;11<xsl:call-template name="WriteData"> <xsl:with-param name="cptPara"> <xsl:value-of select="$cpt"/> </xsl:with-param> </xsl:call-template>&#10;</xsl:if> </xsl:template> <xsl:template name="WriteData"> <xsl:param name="cptPara"/>2GBLV&#32;&#32;&#32;&#32;<xsl:call-template name="Codpev"> <xsl:with-param name="cptPara"> <xsl:value-of select="$cptPara"/> </xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierNumerique"> <xsl:with-param name="LongueurNumVoulue">7</xsl:with-param> <xsl:with-param name="ChampAMapper" select="/ListOfPKT/PKT[position()=$cptPara]/DesCliBlNbr/."/> </xsl:call-template> Please to help me because i don't understand.


Replies (6)

Please register to reply

RE: Problem to génération a text file - Added by Anonymous over 18 years ago

Legacy ID: #3826806 Legacy Poster: Michael Kay (mhkay)

You haven't shown the source document, but I think the most likely explanation is that your stylesheet is copying whitespace from the source document to the result. You can prevent this by using <xsl:strip-space elements="*"/>. The difference with XML Spy is probably because the Microsoft XML parser (incorrectly, in my view) strips whitespace by default.

RE: Problem to génération a text file - Added by Anonymous over 18 years ago

Legacy ID: #3826826 Legacy Poster: stepd (stepd)

I've done the modification but it changes nothing. The file generated seem to be indented. This is the source document : <?xml version="1.0" encoding="UTF-8"?> <ListOfPKT> <PKT> <DesCliBlNbr>5</DesCliBlNbr> <DateRecepEDI>20060717</DateRecepEDI> <ManagingTerritory>LIF</ManagingTerritory> <ShipToCnty>101720</ShipToCnty> </PKT> <PKT> <DesCliBlNbr>4</DesCliBlNbr> <DateRecepEDI>20060717</DateRecepEDI> <ManagingTerritory>TID</ManagingTerritory> <ShipToCnty>85000</ShipToCnty> </PKT> </ListOfPKT>

RE: Problem to génération a text file - Added by Anonymous over 18 years ago

Legacy ID: #3827504 Legacy Poster: Michael Kay (mhkay)

I can't see anything in this code that accounts for the extra whitespace. Perhaps the problem is in the part of the code that you haven't shown. It would help to post a complete source document and stylesheet, so that I can run it and debug it myself. Ideally this should be cut down to the minumum code that demonstrates the problem. I do have a few comments about your XSLT coding. (1) <xsl:template match="/ListOfPKT/PKT"> would be better written as match="PKT". There's no reason to add the extra tests on the parent and grandparent of the PKT element, if they will always be satisfied. (2) <xsl:variable name="cpt"> <xsl:number count="//PKT"/> </xsl:variable> seems to be a rather complicated way of saying <xsl:variable name="cpt" select="position()"/> But I don't think you need this variable at all, as you are only using it to retrieve the current node - if you need to remember the current node, put the node in a variable, rather than its position. (3) <xsl:if test="/ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='TID' or /ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='LIF' or /ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='LIV'"> seems to be a rather complicated way of saying <xsl:if test="ManagingTerritory=('TID', 'LIF', 'LIV')"> (4) <xsl:with-param name="cptPara"> <xsl:value-of select="$cpt"/> </xsl:with-param> is much less efficient than <xsl:with-param name="cptPara" select="$cpt"/> because it constructs a result tree fragment rather than simply passing an integer (and because it's 3 lines of code instead of 1) But in fact, rather than pass the integer, you should be passing the node. The integer is used in this expression to select a node: select="/ListOfPKT/PKT[position()=$cptPara]"/> But that's the context node, so you don't need to pass it as an explicit parameter at all, you can just refer to it in the called template as ".". Michael Kay Saxonica Limited

RE: Problem to génération a text file - Added by Anonymous over 18 years ago

Legacy ID: #3827783 Legacy Poster: stepd (stepd)

This the complete source code : <?xml version="1.0" encoding="UTF-8"?> <!--Feuille XSL qui convertit les accusés de réception des commandes (ordres de livraison - fichiers .PKT) de PKMS vers un fichier à plat au format Roederer--> <!--Auteur : SD (17/07/2006)--> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon"> <xsl:import href="communs.xsl"/> <xsl:strip-space elements="*"/> <xsl:output method="text" indent="no"/> <xsl:template match="/ListOfPKT/PKT"> <!--création de la variable cpt pour connaitre la position du PKT que l'on est en train de traiter--> <xsl:variable name="cpt"> <xsl:number count="//PKT"/> </xsl:variable> <!--test si le ManagingTerritory = TID ou LIF ou LIV car sinon on ne traite pas ce PKT--> <xsl:if test="/ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='TID' or /ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='LIF' or /ListOfPKT/PKT[position()=$cpt]/ManagingTerritory/.='LIV'"> <!--traitement du 1er type d'enregistrement-->01<!--appel du template WriteData qui met à jour les champs du fichier cible--> <xsl:call-template name="WriteData"> <!--On lui passe la variable cpt (pour la même raison que plus haut)--> <xsl:with-param name="cptPara"> <xsl:value-of select="$cpt"/> </xsl:with-param> </xsl:call-template> <!--traitement du 2ème type d'enregistrement-->&#10;11<xsl:call-template name="WriteData"> <xsl:with-param name="cptPara"> <xsl:value-of select="$cpt"/> </xsl:with-param> </xsl:call-template>&#10;</xsl:if> </xsl:template> <xsl:template name="WriteData"> <xsl:param name="cptPara"/>2GBLV&#32;&#32;&#32;&#32;<xsl:call-template name="Codpev"> <xsl:with-param name="cptPara"> <xsl:value-of select="$cptPara"/> </xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierNumerique"> <xsl:with-param name="LongueurNumVoulue">7</xsl:with-param> <xsl:with-param name="ChampAMapper" select="/ListOfPKT/PKT[position()=$cptPara]/DesCliBlNbr/."/> </xsl:call-template> <xsl:if test="/ListOfPKT/PKT[position()=$cptPara]/ManagingTerritory/.='LIF'"> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper" select="/ListOfPKT/PKT[position()=$cptPara]/ShipToCnty/."/> </xsl:call-template> </xsl:if> <xsl:if test="/ListOfPKT/PKT[position()=$cptPara]/ManagingTerritory/.!='LIF'"> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> </xsl:if> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">7</xsl:with-param> <xsl:with-param name="ChampAMapper"/> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template>~<xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">16</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">3</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">10</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">3</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">2</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">2</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">3</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template>~~~~~<xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">30</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">2</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">3</xsl:with-param> <xsl:with-param name="ChampAMapper"/> </xsl:call-template>000<xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">41</xsl:with-param> <xsl:with-param name="ChampAMapper"/> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">3</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template>~~<xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">6</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">6</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">6</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">6</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">10</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">3</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">30</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template>~~~~~<xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">12</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">7</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">30</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">30</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">30</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> <xsl:call-template name="JustifierAlphanumerique"> <xsl:with-param name="LongueurAlphanumVoulue">8</xsl:with-param> <xsl:with-param name="ChampAMapper">~</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template name="Codpev"> <!--Test pour l'info à mettre dans le champ Codpev en fonction du ManagingTerritory--> <xsl:param name="cptPara"/> <xsl:if test="/ListOfPKT/PKT[position()=$cptPara]/ManagingTerritory/.='TID'">GT1&#32;&#32;&#32;&#32;&#32;</xsl:if> <xsl:if test="/ListOfPKT/PKT[position()=$cptPara]/ManagingTerritory/.='LIF'">GP1F&#32;&#32;&#32;&#32;</xsl:if> <xsl:if test="/ListOfPKT/PKT[position()=$cptPara]/ManagingTerritory/.='LIV'">GP1E&#32;&#32;&#32;&#32;</xsl:if> </xsl:template> </xsl:stylesheet> Thank you for your help.

RE: Problem to génération a text file - Added by Anonymous over 18 years ago

Legacy ID: #3827826 Legacy Poster: stepd (stepd)

You can stop to search. I find the reason of the problem. It comes from the comments line just before the Text. I've dropped them and all is ok. Thank you for your remarks, i've modified my code and it's more simple.

RE: Problem to génération a text file - Added by Anonymous over 18 years ago

Legacy ID: #3829164 Legacy Poster: Michael Kay (mhkay)

Glad you solved it. There's a subtle rule here which I overlooked when reading your first code sample: comments are stripped from a stylesheet before stripping whitespace text nodes. So <s> <!--comment-->01<!--comment--> </s> after stripping comments becomes <s> 01 </s> and the whitespace characters become part of the text node containing the text "01", so they are output. It's always a good idea, when you care about formatting, to put literal text in the stylesheet within an <xsl:text> element to prevent this kind of problem.

    (1-6/6)

    Please register to reply