我正在为我的 XSL 添加国际化功能。我见过很多创建dictionary.xml 文件并通过document('dictionary.xml') 将其加载到我的XSL 中的示例。我想做类似的事情,但我不想在磁盘上创建和存储dictionary.xml 文件,我宁愿在服务器启动时从SQL 构建它,并将Document 对象保留在Java 的内存中。然后我想将字典文档作为参数传递给转换器,以便我的 XSL 翻译函数可以使用它。但是,它似乎不起作用。
相关Java代码:
Document dictionary = TranslationDictionary.getDictionaryDocument();
transformer.setParameter("dictionary", dictionary);
词典文档内容:
<dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<translatedString dictionaryId="BASIC_DETAILS">
<language id="es" value="Detalles Básicos"/>
</translatedString >
<translatedString dictionaryId="VEHICLE_INFORMATION">
<language id="es" value="Información del Vehículo"/>
</translatedString >
<translatedString dictionaryId="STRUCTURE">
<language id="es" value="Estructura"/>
</translatedString >
<translatedString dictionaryId="DRIVER_INFORMATION">
<language id="es" value="Información del Conductor"/>
</translatedString >
<translatedString dictionaryId="MAINTENANCE_AND_FEUL">
<language id="es" value="Mantenimiento & Combustible"/>
</translatedString >
<translatedString dictionaryId="PURCHASING">
<language id="es" value="Compra"/>
</translatedString >
</dictionary>
XSL 文件:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://www.test.com">
<xsl:param name="dictionary"/>
<xsl:param name="language" select="'es'"/>
<xsl:template match="/">
<xsl:message>
<xsl:copy-of select="$dictionary/dictionary/translatedString[@dictionaryId='BASIC_DETAILS']/language[@id='es']/@value"/>
</xsl:message>
</xsl:template>
但我什么也没得到。我尝试只复制 $document/document 来确认我没有遇到 xpath 问题,但事实并非如此,因为这给了我一份完整文档的副本。就好像 XSL 将 $dictionary 视为字符串而不是节点。有什么线索吗?
Use a URIResolver
而不是参数。首先,像这样创建解析器:
public class DocURIResolver implements URIResolver {
final Map<String, Document> documents;
public DocURIResolver(final Map<String, Document> documents) {
this.documents = documents;
}
public Source resolve(final String href, final String base) {
final Document doc = documents.get(href);
return (doc != null) ? new DOMSource(doc) : null;
}
}
像这样使用它:
Document dictionary = TranslationDictionary.getDictionaryDocument();
Map<String, Document> docs = new HashMap<String, Document>();
docs.put("dictionary", dictionary);
// transformer is your javax.xml.transform.Transformer
transformer.setURIResolver(new DocURIResolver(docs));
并按名称在样式表中引用它:
<xsl:variable name="dict" select="document('dictionary')"/>
当然,这只是一个玩具示例。你可以让你的URIResolver
功能齐全。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)