将文档作为参数传递给Java中的XSL

将文档作为参数传递给Java中的XSL

本文介绍了将文档作为参数传递给Java中的XSL Translation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的XSL进行额外的国际化。我已经看到很多创建dictionary.xml文件并通过文档('dictionary.xml')将其加载到我的XSL中的示例。我想做类似的事情,但我不想在磁盘上创建和存储dictionary.xml文件,我宁愿在服务器启动时从SQL构建它,并将Document对象保存在Java内存中。我想将字典文档作为参数传递给变换器,以便我的XSL转换函数可以使用它。但是,它似乎没有用。

I'm working on addition internationalization to my XSL. I've seen plenty of examples of creating a dictionary.xml file and loading it into my XSL via document('dictionary.xml'). I want to do something similar, but I don't want to create and store the dictionary.xml file on disk, I'd rather build it from SQL at server startup and keep the Document object in memory in Java. I'd like to then pass the dictionary document as a parameter to the transformer so that my XSL translation function can use it. However, it doesn't seem to be working.

相关的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&#225;sicos"/>
    </translatedString >
    <translatedString dictionaryId="VEHICLE_INFORMATION">
        <language id="es" value="Informaci&#243;n del Veh&#237;culo"/>
    </translatedString >
    <translatedString dictionaryId="STRUCTURE">
        <language id="es" value="Estructura"/>
    </translatedString >
    <translatedString dictionaryId="DRIVER_INFORMATION">
        <language id="es" value="Informaci&#243;n del Conductor"/>
    </translatedString >
    <translatedString dictionaryId="MAINTENANCE_AND_FEUL">
        <language id="es" value="Mantenimiento &amp; 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视为字符串而不是节点一样。任何线索?

But I get nothing. I've tried just doing a copy of $document/document to confirm that I'm not having an xpath issue, and its not that, because that gives me a copy of the full document. It's as if the XSL is seeing $dictionary as a string instead of a node. Any clues?

推荐答案

好的,我制作了代码的骨架副本。这听起来很奇怪,但是在java代码中创建字典文档之后,在将其设置为变换器的参数之前,只需调用方法:

Ok, I made a skeleton copy of your code. This is going to sound bizarre, but after you create your dictionary document in the java code, and before you set it as a parameter for the transformer, just invoke the method:

dictionary.getDocumentElement();

那么它有效!看起来像撒克逊人正在处理文件参数的方式中的一个错误,它需要某种初始化而尚未完成?我没有深入研究调试器。

then it works! Looks like a bug in the way saxon is handling a parameter thats a document, where it requires some kind of initialisation which hasnt been done ? I'm not digging into the debugger.

这篇关于将文档作为参数传递给Java中的XSL Translation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:15