How to access xsl:param in an attribute of xsl:output?

后端 未结 2 1174
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 02:32

I want to allow the transformer to set a param in my stylesheet to specify how many spaces of indentation are desired. I have tried all of the suggestions by @Dimitre Novatchev

相关标签:
2条回答
  • 2021-01-27 02:49

    As @Mads Hansen already said it does not appear that Xalan supports attribute value templates for the value of the xalan:indent-amount attribute.

    I found that setting the property on the transformer itself does work though. The nice thing about this is that I don't have to create/modify any files.

    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    

    Credit goes to @Flynn1179

    0 讨论(0)
  • 2021-01-27 03:08

    It does not appear that Xalan supports attribute value templates for the value of the xalan:indent-amount attribute.

    I get a warning message that attribute value templates are not supported in xsl:output in XSLT 1.0. Apparently, they are supported in 1.1 (which should not be used, as it was abandoned by the W3C), but Xalan does not appear to resolve the parameter value in the AVT in that particular attribute.

    When I attempt to use the parameter value in an AVT, it returns the error:

    E For input string "{$indent}"
    

    One possible workaround would be to use entities and generate a DTD. Rather than pass in the indent value as a param, have your XSLT reference a DTD. Generate the DTD file with the indent value that you need and then invoke the XSLT.

    For example, create a DTD file like this (assume named "indent.dtd"):

    <!ENTITY indent "10" >
    

    And then reference the DTD in your XSLT like this (assume that indent.dtd is located in the same folder, or you could adjust the path):

    <!DOCTYPE xsl:stylesheet SYSTEM "indent.dtd">
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output indent="yes" method="xml" omit-xml-declaration="yes" 
                   xalan:indent-amount="&indent;"/> 
    
    </xsl:stylesheet>
    

    Another workaround would be to generate an XSLT with the desired value for the xalan:indent-amount first, and then transform your XML with the newly generated XSLT.

    0 讨论(0)
提交回复
热议问题