Dynamically setting the <xsl:output> encoding
Added by Anonymous almost 20 years ago
Legacy ID: #2877479 Legacy Poster: vanfleet (vanfleet)
I was wondering if someone here could help me. Im using XSLT 2.0 with Saxonb 8.1.1 to process single XML documents into multiple HTML files, and Im using the <xsl:result-document> element to generate these html files. The encoding of these html documents needs to be dynamically set based on a language attribute on the root element of the XML document. For example if language=english then the output encoding should be set to iso-8859-1, if language= japanese then encoding should be set to shift_jis etc. I have determined that I can do this with JAXP with something like the following: public void processXML(String xmlFile, String xslFile, String language) throws TransformerException, TransformerConfigurationException { String encoding; // Create a transform factory instance. TransformerFactory tfactory = TransformerFactory.newInstance(); // Create a transformer for the stylesheet. Transformer transformer=tfactory.newTransformer(new StreamSource(xslFile)); if (language.equals("japanese")) encoding="shift_jis"; else if (language.equals("korean")) encoding="ks_c_5601"; else encoding="iso-8859-1"; transformer.setOutputProperty(OutputKeys.METHOD, "html"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); // Transform the source XML to System.out. transformer.transform(new StreamSource(xmlFile), new StreamResult(System.out)); } However, my problem is that when the HTML is generated by way of the XSTL <xsl:result-document> element the encoding specified by the Java setOutputProperty method is not picked up in the final output. But rather it picks up the default value of UTF-8. If the result is sent to System.out then the correct encoding is used. Any help anyone would be willing to offer would be greatly appreciated. Thanks,
Replies (2)
RE: Dynamically setting the <xsl:output> enco - Added by Anonymous almost 20 years ago
Legacy ID: #2877491 Legacy Poster: Michael Kay (mhkay)
The serialization attributes of xsl:result-document are attribute value templates, so you can say xsl:result-document encoding="{$encoding}" These override anything in xsl:output. Michael Kay
RE: Dynamically setting the <xsl:output> encoding - Added by Anonymous almost 20 years ago
Legacy ID: #2878026 Legacy Poster: vanfleet (vanfleet)
That was exactly what I was looking for, I didnt realize that <xsl:result-document> would allow the encoding attribute. I appreciate your help. Thanks, David Vanfleet
Please register to reply