我正在为我的XSL进行额外的国际化.我已经看到很多创建dictionary.xml文件并通过文档('dictionary.xml')将其加载到我的XSL中的示例.我想做类似的事情,但我不想在磁盘上创建和存储dictionary.xml文件,我宁愿在服务器启动时从SQL构建它,并将Document对象保存在Java内存中.我想将字典文档作为参数传递给变换器,以便我的XSL转换函数可以使用它.但是,它似乎没有起作用.
相关的Java代码:
Document dictionary = TranslationDictionary.getDictionaryDocument();
transformer.setParameter("dictionary", dictionary);
Run Code Online (Sandbox Code Playgroud)
字典文件内容:
<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
但我一无所获.我试过只做一个$ document/document的副本来确认我没有xpath问题,而不是那个,因为这给了我一份完整的文档.就像XSL将$ dictionary视为字符串而不是节点一样.有线索吗?
使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
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));
Run Code Online (Sandbox Code Playgroud)
并按名称在样式表中引用它:
<xsl:variable name="dict" select="document('dictionary')"/>
Run Code Online (Sandbox Code Playgroud)
当然,这只是一个玩具的例子.您可以URIResolver
根据需要使您的功能全面.
好的,我制作了您的代码的骨架副本。这听起来很奇怪,但是在您在 java 代码中创建字典文档之后,并且在将其设置为转换器的参数之前,只需调用该方法:
dictionary.getDocumentElement();
Run Code Online (Sandbox Code Playgroud)
然后就可以了!看起来撒克逊处理文档参数的方式存在错误,它需要某种尚未完成的初始化?我不会深入研究调试器。
归档时间: |
|
查看次数: |
4521 次 |
最近记录: |