Pass absolute file path from java code to xslt doc

2020-04-16 17:45发布

问题:

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')).