1

I am a trying to pass the value of a shell variable to an xsltproc instance. The file text.xsl contains:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="file:/Users/robertramey/WorkingProjects/modular-boost/tools/boostbook/xsl/docbook.xsl"/>
<xsl:import href="file:$BOOST_ROOT/tools/boostbook/xsl/docbook.xsl"/>
</xsl:stylesheet>


#functions as expected
$echo $BOOST_ROOT
/Users/robertramey/WorkingProjects/modular-boost


#passes on first import as expected.
#but fails on the second as expect as BOOST_ROOT is not set
$xsltproc --maxdepth 6000 --xinclude --nonet test.xsl
warning: failed to load external entity
"file:$BOOST_ROOT/tools/boostbook/xsl/docbook.xsl"


#passes on the first import as expected
#fails on the second - NOT expected

$xsltproc --maxdepth 6000 --stringparam BOOST_ROOT "/Users/robertramey/WorkingProjects/modular-boost" --xinclude --nonet test.xsl
warning: failed to load external entity
"file:$BOOST_ROOT/tools/boostbook/xsl/docbook.xsl" compilation error: file test.xsl line 4 element import
xsl:import : unable to load file:$BOOST_ROOT/tools/boostbook/xsl/docbook.xsl

Why is this? What do I have to do to make this work?

$cat bb2db.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:import href="https://www.boost.org/tools/boostbook/xsl/docbook.xsl"/> </xsl:stylesheet>

which works. But it relies on a global URL to find the file to import. This makes my script dependent on having an internet connection which I'd prefer to avoid. I'd like to refer to a local directory:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:import href=file:"$BOOST_ROOT/tools/boostbook/xsl/docbook.xsl"/> </xsl:stylesheet>

Since the shell variable holds the base path of the desired xml stylysheet I want to import.

6
  • 1
    I don't think parameterizing an xsl:import is possible with XSLT 1. With XSLT 3 (like SaxonC 12 HE) you might be able to use a static parameter (<xsl:param name="BOOST_ROOT" static="yes" as="xs:string" select="'/Users/robertramey/WorkingProjects/modular-boost'"/>) and a shadow attribute (<xsl:import _href="file:{$BOOST_ROOT}/tools/boostbook/xsl/docbook.xsl"/>). Commented Jul 28, 2024 at 22:53
  • Can you explain what you are trying to achieve by your attempt to parameterise the xsl:import? What is the reason why you don't just import the file directly? Commented Jul 29, 2024 at 1:22
  • I expanded the question to include this information. Thanks for helping me out with this! Commented Jul 29, 2024 at 19:15
  • 1
    I think your question could be divided into two: (1) how to pass a parameter to an XSLT stylesheet and (2) how to use the parameter in xsl:import. Since the answer to #2 is that you can't (because the href attribute of xsl:import is not an attribute value template) the first part becomes moot. Commented Jul 29, 2024 at 19:36
  • I am not a heavy xsltproc users but I would suggest to check whether using a catalog linux.die.net/man/1/xsltproc can't help you resolve a HTTPS URL like https://www.boost.org/tools/boostbook/xsl/docbook.xsl to your local file URL. Or of course consider moving to XSLT 3, both SaxonC 12 for C/C++ saxonica.com/saxon-c/index.xml or the Node.js SaxonJS/xslt3 command line tool npmjs.com/package/xslt3 should support the approach with a static parameter and a shadow attribute. Commented Jul 30, 2024 at 6:28

1 Answer 1

0

Looks like I may have it!!!!: xsltproc --maxdepth 6000 --path "$BOOST_ROOT/tools/boostbook/xsl" --xinclude --nonet test.xsl

test.xsl contains:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:import href="xsl/docbook.xsl"/> </xsl:stylesheet>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.