If you perform the transformation in your own application you can use another approach. One that requires some coding but leaves your stylesheets less cluttered.
the stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet SYSTEM "i18n/humanreadable.ent">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...>
&text1; &text2;
(You can choose whatever name you fancy for the i18n directory, just keep in mind it plays a special role.)
humanreadable.ent:
<!ENTITY text1 "Hello">
<!ENTITY text2 "world!">
So far this is still a good old & valid XSLT. But while it keeps the stylesheet more readable it does not give you the much desired multi-language support. For that you need to do some coding.
Customize the document builder which you parse the stylesheet file with; assign an entity resolver to it:
Source getStylesheetSource(String stylesheetFilename, EntityResolver entityResolver) throws ... {
DocumentBuilder docBuilder = getDomFactory().newDocumentBuilder();
docBuilder.setEntityResolver(entityResolver);
Document stylesheet = docBuilder.parse(new FileInputStream(new File(stylesheetFilename)));
return new DOMSource(stylesheet);
}
This entity resolver gets called every time a relative URL/path is encountered when your stylesheet is being parsed into a document.
When this happens check whether the path starts with your magic prefix (your special directory), and translate this prefix into a path pointing to the humanreadable.ent of your desired language.
final String targetLanguage = figureOutDesiredLanguage(...);
EntityResolver entityResolver = new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
if (pointsToMySpecialFolder(systemId)) {
String lang = targetLanguage;
String i18n = insertLangIntoThePath(systemId, lang);
return new InputSource(new FileInputStream(new File(i18n)));
}
return null;
}
};
Source stylesheet = getStylesheetSource("stylesheet.xslt", entityResolver);
Result result = new SAXResult(...);
Transformer transformer = transformerFactory.newTransformer(stylesheet);
transformer.transform(new DOMSource(inputXml), result);
The drawbacks are obvious: you need to do some coding outside of XML/XSLT, and your XSLT stylesheet is multi-lingual only when used within your super-special application.
The benefit is no extra tag soup in my (already rather dense) XSLT stylesheets.