Pass absolute file path from java code to xslt document()

你说的曾经没有我的故事 提交于 2019-12-20 02:56:41

问题


In my xslt I'd like to look up an xml file. I need to pass the path to this file from java code.I have the followings:

...
Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
transformer.setParameter("mypath", "/home/user/repository");

xslt:

<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:param name="mypath"/>

  ...
  <xsl:template match="connection[@id]">
    <xsl:variable name="lookupStore" select="document('$mypath/myfile.xml')/connections"/>
    <xsl:copy>
      <xsl:apply-templates select="$lookupStore">
        <xsl:with-param name="current" select="."/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  ...
<xsl:transform>

The problem is that I want to pass an absolute "base" path to the xsl, which I want to combine with the actual xml file name (myfile.xml). It seems to me that document considers file parameters relative to the location of the xsl. Furthermore I remarked that the parameter is not picked up from the java code. I use JABX with the default Xalan XSLT processor (1.0) I tried many variations of passing the parameters based on the other SO posts, but no success.


回答1:


You need to construct a string then with the complete file URL: document(concat('file://', $mypath, '/myfile.xml')).



来源:https://stackoverflow.com/questions/30153433/pass-absolute-file-path-from-java-code-to-xslt-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!