Search code examples
stringxsltxpathparametersxalan

Select the string of an XSLT param given at command line


I am using XSLT params to set an absolute path in an attribute at runtime using Xalan-C. Basically, my input XML is something like this :-

<root xmlns="initial">
  <!-- document goes here -->
</root>

My stylesheet is :-

<xsl:stylesheet version="1.0" xmlns:s="initial" xmlns="final" />

  <xsl:param name="default_data_location">/path/to/some/location</xsl:param>

  <xsl:template match="//s:*">
    <xsl:element name="{local-name()}" namespace="final">
      <xsl:attribute name="dataLocation">
        <xsl:value-of select="concat($default_data_location, '/datafile')"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- rest of the stylesheet -->

</xsl:stylesheet>

Thus, my desired output XML when I run it as :-

Xalan foo.xml foo.xsl 

should be (this is the part that works) :-

<root xmlns="final" dataLocation="/path/to/some/location/datafile">
  <!-- document goes here -->
</root>

And when I run it as :-

Xalan -p default_data_location /some/other/path foo.xml foo.xsl

it should be (and this is the part that doesn't work) :-

<root xmlns="final" dataLocation="/some/other/path/datafile">
  <!-- document goes here -->
</root>

If I try to set this param at the command line, however, it gives me the following XML :-

<root xmlns="final" dataLocation="/datafile">
  <!-- document goes here -->
</root>

What should I be doing?


Solution

  • The parameter value seems to be an XPath expression so you need to make sure you pass in an XPath string and you might need to double quotes to make sure the command line shell does not get into your way so doing Xalan -p default_data_location "'/some/other/path'" foo.xml foo.xsl should work. At least that's my reading of the documentation at http://xml.apache.org/xalan-c/commandline.html, I don't have Xalan-C to test.