[XSLT] encoding UTF-8 et accents
Bonjour j'ai un fichier XML contenant des accents, lors de la transformation XSL j'ai l'erreur :
javax.xml.transform.TransformerException: Invalid byte 2 of 3-byte UTF-8 sequence.
alors que dans mon fichier XSL j'ai bien :
Code:
1 2 3 4 5
| <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:fox="http://xml.apache.org/fop/extensions" xmlns:xi="http://www.w3.org/2001/Xinclude">
<xsl:output method="xml" encoding="UTF-8" indent="yes"></xsl:output> |
Utilisation d'un fichier xsl de transformation avec fop 0.2
Pour plus d'info, j'ai un fichier xsl de transformation avec fop 0.2, me problème peut il venir de mauvaise utilisation des librairies fop????
Code java, fichier xml et fichier xsl!
Bonjour,
dès que mon fichier xml contient des accents et même si cette donnée n'est pas lise au niveau du xsl, lors de la transformation j'ai l'exception suivante:
Code:
1 2 3 4 5 6 7 8 9
|
; SystemID: file:///C:/Documents%20and%20Settings/fbengazd/Bureau/BrouillonFutur/FTContractsImpression.xml; Line#: 88; Column#: 38
javax.xml.transform.TransformerException: Invalid byte 2 of 3-byte UTF-8 sequence.
at org.apache.xalan.transformer.TransformerImpl.fatalError(Unknown Source)
at org.apache.xalan.transformer.TransformerImpl.transform(Unknown Source)
at org.apache.xalan.transformer.TransformerImpl.transform(Unknown Source)
at org.apache.xalan.transformer.TransformerImpl.transform(Unknown Source)
at com.lhs.ccb.nonkernel.test.controller.ExampleXML2PDF.convertXML2PDF(ExampleXML2PDF.java:85)
at com.lhs.ccb.nonkernel.test.controller.ExampleXML2PDF.main(ExampleXML2PDF.java:118) |
ma classe d'impression pdf est la suivante:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
public class ExampleXML2PDF {
public void convertXML2PDF(File xml, File xslt, File pdffile)
throws IOException, FOPException, TransformerException {
// Construct driver
Driver driver = new Driver();
// Setup logger
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);
// Setup Renderer (output format)
driver.setRenderer(Driver.RENDER_PDF);
// Setup output
//ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream out = new FileOutputStream(pdffile);
// Charset utf8 = Charset.forName("utf8");
// Writer fw = new OutputStreamWriter(new FileOutputStream(xml), utf8);
// Reader xslReader = new InputStreamReader(new FileInputStream(xslt), utf8);
//
try {
driver.setOutputStream(out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(
xslt));
// Setup input for XSLT transformation
Source src = new StreamSource(xml);
// Resulting SAX events (the generated FO) must be piped through to
// FOP
Result res = new SAXResult(driver.getContentHandler());
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,"UTF-8");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.METHOD,".xml");
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
/*pResponse.setContentType("application/pdf");
pResponse.setHeader("Content-Disposition", "attachment; filename=Contrat.pdf");
pResponse.setContentLength(out.size());
pResponse.getOutputStream().write(out.toByteArray());
pResponse.getOutputStream().flush();
pResponse.getOutputStream().close();*/
} finally {
out.close();
}
} |
mon fichier xml est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
<contractsImpression>
<baseDir>/products/futbdev6/ix_001/TomCat_CustCare/webapps/CustCare/WEB-INF</baseDir>
<customerInformations>
<custSiren>302012999</custSiren>
<custName>TestCustomerInformations</custName>
<custNaf>11111</custNaf>
<administrativeContact>
<name>TestContactInfo</name>
<address> rue test</address>
<zip>75015</zip>
<city>paris</city>
<tel1>0633333333</tel1>
</administrativeContact>
</customerInformations>
<costCenterInformations>
<costcenterAddress>
<name>TestContactInfo</name>
<address> rue test</address>
<zip>75015</zip>
<city>paris</city>
<tel1>0633333333</tel1>
</costcenterAddress>
<paymentMean>TestPaymentMean</paymentMean>
<billType>TestBillType</billType>
<billFormat></billFormat>
</costCenterInformations>
<accountOwnerInformations>
<accountOwner>Test</accountOwner>
<address> rue test</address>
<zip>75015</zip>
<city>paris</city>
</accountOwnerInformations>
<paymentArrangementInformations>
<accountNumber>19249251893</accountNumber>
<ribKey>46</ribKey>
<bankName>CityBank</bankName>
<bankAddress> rue bank</bankAddress>
<bankCode>40168</bankCode>
<agencyCode>00019</agencyCode>
<bankZip>75015</bankZip>
<bankCity>paris</bankCity>
</paymentArrangementInformations>
<contracts>
<contract>
<marketId>1</marketId>
<market>Services mobiles</market>
<rateplan>Entourage Privilege</rateplan>
<line>33990001159</line>
<engagement>Engagement 24 mois</engagement>
<services>
<service>
<description>Usage Telephone Abonnement</description>
</service>
<service>
<description>Usage Messagerie ecrite SMS</description>
</service>
<service>
<description>Renvoi dappel</description>
</service>
<service>
<description>Usage Multi-media (envoi MMS)</description>
</service>
<service>
<description>Double appel</description>
</service>
<service>
<description>Futur Monde</description>
</service>
<service>
<description>Usage Visiophonie</description>
</service>
<service>
<description>Repondeur</description>
</service>
<service>
<description>Test3</description>
</service>
<service>
<description>Test2</description>
</service>
<service>
<description>Test1</description>
</service>
<service>
<description>Usage Appels durgence</description>
</service>
<service>
<description>A la seconde dès la 1 seconde</description>
</service>
</services>
<pin1>4017</pin1>
<pin2>3093</pin2>
<material>
<description>NOKIA</description>
<price>20</price>
</material>
</contract>
</contracts>
</contractsImpression> |
et le fichir xsl:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xml.apache.org/fop/extensions" xmlns:xi="http://www.w3.org/2001/Xinclude">
<xsl:output method="xml" encoding="UTF-8" indent="yes"></xsl:output>
<xsl:variable name="resourceDir" select="contractsImpression/baseDir"/>
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="page1"
page-height="29.7cm" page-width="21cm">
<fo:region-body margin-top="3cm"
margin-bottom="3cm" />
<fo:region-before margin-bottom="0.5cm" />
<fo:region-after />
</fo:simple-page-master>
<fo:simple-page-master master-name="all"
page-height="29.7cm" page-width="21cm">
<fo:region-body margin-top="1cm"
margin-bottom="3cm" />
<fo:region-before margin-bottom="0.5cm" />
<fo:region-after extent="2.5cm" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="page1">
<fo:static-content flow-name="xsl-region-after">
<fo:block text-align="left" font-size="10pt"
color="lightsteelblue" />
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block text-align="left" border="1px solid black"
margin-left="1cm" margin-right="1cm">
<xsl:apply-templates
select="contractsImpression/costCenterInformations" />
</fo:block>
</fo:flow>
</fo:page-sequence>
<fo:page-sequence master-reference="all">
<fo:static-content flow-name="xsl-region-after">
<fo:block text-align="left" font-size="10pt"
color="lightsteelblue" />
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:block font-size="7pt" padding-left="3pt"
font-weight="bold" margin-left="0.3cm" margin-right="17cm"
color="white" background-color="darkblue">
SIGNATURES
</fo:block>
<fo:block border="1px solid slateblue"
padding-left="3pt" font-size="10pt" color="darkblue"
margin-left="0.3cm" margin-right="0.5cm" padding-top="2pt"
padding-bottom="2pt">
<fo:block text-align="left">
<fo:block font-size="5.5pt">
L'Abonné déclare avoir pris
connaissance et accepter les
Conditions générales d'accés aux
services et
</fo:block>
<fo:block font-size="5.5pt">
le catalogue tarifaire, disponibles sur le site
</fo:block>
<fo:block font-size="8pt"
padding-top="3pt">
Nom et Prénom du Signataire:
</fo:block>
<fo:block font-size="8pt"
padding-top="5pt">
Signature et
</fo:block>
<fo:block font-size="8pt">
Cachet du Signataire:
</fo:block>
<fo:block font-size="8pt"
padding-top="5pt" padding-bottom="15pt">
Date:
</fo:block>
</fo:block>
<fo:block position="absolute"
font-size="8pt" left="300pt">
<fo:block padding-bottom="2pt">
Nom du Partenaire:
</fo:block>
<fo:block>Signature et</fo:block>
<fo:block padding-bottom="2pt">
Cachet du Partenaire:
</fo:block>
<fo:block padding-bottom="10pt">
Date:
</fo:block>
<fo:block padding-bottom="2pt">
N° du Vendeur:
<fo:inline padding-left="50pt"
padding-bottom="2pt">
Nom du vendeur:
</fo:inline>
</fo:block>
</fo:block>
</fo:block>
<fo:block text-align="left" margin-left="0.3cm"
padding-top="5pt" font-size="5.5pt" color="darkblue">
Conformément à la loi n° 78-17 du 6 janvier
1978, les informations nominatives
recueillies dans le cadre du contrat peuvent
donner lieu à l'excercie du droit d'accès,
de modification, d'opposition à
communication et de suppression
<fo:block>
à l'adresse mentionnée à l
</fo:block>
<fo:block>ou de ses partenaires.</fo:block>
<fo:block>
Si vous ne souhaitez pas être mentionné
parmi les références,
cochez cette case :
</fo:block>
</fo:block>
<fo:block font-size="7pt" padding-left="3pt"
margin-top="0.5cm" margin-left="0.3cm" margin-right="10cm"
color="white" font-weight="bold" background-color="darkblue">
MANDAT DE PRESELECTION (Services de
Téléphonie Fixe)
</fo:block>
<fo:block text-align="left" font-size="7pt"
color="darkblue" border="1px solid slateblue" margin-left="0.3cm"
margin-right="0.5cm" background-color="lavender">
<fo:block margin-left="3pt">
L'Abonné autorise
</fo:block>
</fo:block>
<fo:block font-size="7pt" margin-top="0.5cm"
padding-left="3pt" margin-left="0.3cm" margin-right="17cm"
color="white" font-weight="bold" background-color="darkblue">
ANNEXES
</fo:block>
<fo:block text-align="left" font-size="7pt"
color="darkblue" border="1px solid slateblue" margin-left="0.3cm"
margin-right="0.5cm">
<fo:block margin-left="3pt">
Formulaire inscription aux annuaires
Mandat ADSL
AFNIC
Formulaire Coût Partagé
</fo:block>
</fo:block>
<fo:block font-size="7pt" margin-top="0.5cm"
margin-left="0.3cm" margin-right="15cm" color="white"
font-weight="bold" padding-left="3pt"
background-color="darkblue">
PIECES JUSTIFICATIVES
</fo:block>
<fo:block text-align="left" font-size="7pt"
color="darkblue" border="1px solid slateblue" margin-left="0.3cm"
margin-right="0.5cm">
<fo:block margin-left="3pt">
Extrait K-bis ou Certificat des status
déposés
RIB ou RCP ou RICE
Copie de la pièce d'identité du
signataire
Factures Opérateurs
</fo:block>
</fo:block>
<fo:block font-size="5.5pt" margin-left="0.3cm"
color="darkblue">
Texte d'autorisation
</fo:block>
</fo:block>
<fo:block margin-top="0.5cm" font-size="7pt"
margin-left="0.3cm" padding-left="1pt" margin-right="16cm"
color="white" background-color="darkblue" font-weight="bold">
AUTORISATION DE PRELEVEMENT
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="customerInformations">
<fo:block>
<fo:block font-size="13pt" padding-top="2pt"
padding-bottom="6pt" font-weight="bold">
COORDONNEES
</fo:block>
<fo:block font-size="10pt">
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
SIREN/SIRET:
</fo:inline>
<xsl:value-of select="custSiren" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">Adresse:</fo:inline>
<xsl:value-of
select="administrativeContact/address" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Code postal:
</fo:inline>
<xsl:value-of select="administrativeContact/zip" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Nom contact:
</fo:inline>
<xsl:value-of select="administrativeContact/name" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">Tel1:</fo:inline>
<xsl:value-of select="administrativeContact/tel1" />
</fo:block>
</fo:block>
<fo:block position="absolute" top="19pt" left="200pt"
font-size="10pt">
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
RAISON SOCIALE :
</fo:inline>
<xsl:value-of select="custName" />
<fo:inline padding-left="40pt">
NAF :
<fo:inline>
<xsl:value-of select="custNaf" />
</fo:inline>
</fo:inline>
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">Ville :</fo:inline>
<xsl:value-of select="administrativeContact/city" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">Email :</fo:inline>
<xsl:value-of select="administrativeContact/email" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">Tel2 :</fo:inline>
<xsl:value-of select="administrativeContact/tel2" />
<fo:inline padding-left="40pt">
Fax :
<fo:inline>
<xsl:value-of
select="administrativeContact/fax" />
</fo:inline>
</fo:inline>
</fo:block>
</fo:block>
</fo:block>
</xsl:template>
<xsl:template match="costCenterInformations">
<fo:block>
<fo:block font-size="13pt" padding-top="2pt"
padding-bottom="6pt" font-weight="bold">
MODALITES DE FACTURATION:
</fo:block>
<fo:block position="absolute" left="200pt"
font-size="10pt">
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Adresse de Facturation:
</fo:inline>
<xsl:value-of select="costcenterAddress/address" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Code Postal:
</fo:inline>
<xsl:value-of select="costcenterAddress/zip" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Mode de Paiement:
</fo:inline>
<xsl:value-of select="paymentMean" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Type de Facture:
</fo:inline>
<xsl:value-of select="billType" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">Ville:</fo:inline>
<xsl:value-of select="costcenterAddress/city" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Délai de Paiement:
</fo:inline>
<xsl:value-of select="paymentTime" />
</fo:block>
<fo:block padding-bottom="2pt">
<fo:inline font-weight="bold">
Format de facture:
</fo:inline>
<xsl:value-of select="billFormat" />
</fo:block>
</fo:block>
</fo:block>
</xsl:template>
</xsl:stylesheet> |
Exception lors de la transformation
Pour info, le problème survient même si la donnée contenant des accents n'est pas lus par le fichier xsl.
Il me semble que le porblème survient lors de la transformation...
:(
Fichier XML généré automatiquement
Bonjour,
dans mon cas mon fichier XML est généré automatiquement à partir du java, j'ai pas la main dessus donc je dois gérer ça autrement.
Merci je vais tester
Exception : javax.xml.transform.TransformerException: java.io.UTFDataFormatException:
alors là j'avoue que je comprends plus rien:
dans mon code java j'ai essayé de faire comme suivant:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| //TODO delete later :DEBUG : affiche dans System.out le résultat de la sérialisation
//xstream.toXML(dataToSerialize, System.out);
File xmlfile = new File(xmlFilePath);
File xsltfile = new File(xslFilePath);
//TODO new 24/11/2008
FileOutputStream fos = new FileOutputStream(xmlfile);
xstream.toXML(dataToSerialize, fos);
logger.info("DEBUG 1111");
// Construct driver
Driver driver = new Driver();
// Setup Renderer (output format)
driver.setRenderer(Driver.RENDER_PDF);
// Setup output
ByteArrayOutputStream out = new ByteArrayOutputStream();
logger.info("DEBUG 2222");
try {
driver.setOutputStream(out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(
xsltfile));
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
// Resulting SAX events (the generated FO) must be piped through to
// FOP
Result res = new SAXResult(driver.getContentHandler());
logger.info("DEBUG 3333");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,"ISO-8859-1");
//transformer.setOutputProperty(javax.xml.transform.OutputKeys.METHOD,".html");
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
pResponse.setContentType("application/pdf");
pResponse.setHeader("Content-Disposition", "attachment; filename=Contrat.pdf");
pResponse.setContentLength(out.size());
pResponse.getOutputStream().write(out.toByteArray());
pResponse.getOutputStream().flush();
pResponse.getOutputStream().close();
logger.info("DEBUG 4444");
}
finally {
out.close();
} |
dans mon fichier xsl l'entete est :
Code:
1 2 3 4 5
| <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:fox="http://xml.apache.org/fop/extensions" xmlns:xi="http://www.w3.org/2001/Xinclude">
<xsl:output method="xml" encoding="ISO-8859-1" indent="yes"></xsl:output> |
et ça passe tjrs pas!!!!
Solution au moment de la sérialisation objet en XML
la solution était au moment de la sérialisation xml en objet comme suivant:
Code:
XStream xstream = new XStream(new DomDriver("UTF-8"));