Passing a parameter to a saxon style sheet

  • I use net.sf.saxon.TransformerFactoryImplto convert multiple XML files.
  • I use the collection function to get the XML files.

What I want to do and I don’t know if it’s possible is to pass the variable to the stylesheet, which contains the path to the directory where my XML files are stored. I want to pass this variable from my Java code.

    <xsl:for-each 
        select="for $x in(collection('MYVAR?select=*.xml;recurse=yes'))
                    return saxon:discard-document($x)//testsuites">
+3
source share
2 answers

Just use

<xsl:for-each select="for $x in(collection(concat($MYVAR, '?select=*.xml;recurse=yes')))return saxon:discard-document($x)//testsuites">

Please note that MYVAR should be the URL of the file, not the directory of the directory (platform dependent).

[edit] In your XSLT you need

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:param name="MYVAR"/>

  ...

</xsl:stylesheet>

Java http://download.oracle.com/javase/6/docs/api/javax/xml/transform/Transformer.html#setParameter%28java.lang.String,%20java.lang.Object%29, .

transformer.setParameter("MYVAR", "file:///C:/dir/subdir/dir");
+2

:

<xsl:param name="MYVAR" />

Java, , :

transformer.setParameter("MYVAR", 'file:/some/folder');

, :

<xsl:for-each select="for $x in
                        (collection(concat($MYVAR, '?select=*.xml;recurse=yes')))
                          return saxon:discard-document($x)//testsuites">
0

Source: https://habr.com/ru/post/1789981/


All Articles