xsl-list
[Top] [All Lists]

Re: [xsl] Dynamic DTD declaration in output

2019-05-15 01:18:32
Following is one approach that I can come up with (tested with Xalan-J),

Assuming there's following XSLT stylesheet (main.xsl),

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

                         version="1.0">

    <xsl:output doctype-system="/path/to/docs.dtd" method="xml"
encoding="UTF-8" />

    <xsl:template match="test">
         <!-- stylesheet logic -->
    </xsl:template>

</xsl:stylesheet>

Following is a pre-process stylesheet (preprocess.xsl. uses an identity
transform, and modifying the value of attribute xsl:output/@doctype-system
via a stylesheet parameter),

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

                         version="1.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:param name="DTDPath"/>

    <xsl:template match="node() | @*">
      <xsl:copy>
         <xsl:apply-templates select="node() | @*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="@doctype-system">
       <xsl:attribute name="doctype-system">
          <xsl:value-of select="$DTDPath"/>
       </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>

We can then run the transformation as follows,

java org.apache.xalan.xslt.Process -in main.xsl -xsl preprocess.xsl -param
DTDPath mypath.dtd

The output produced by above transformation is following,

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"; version="1.0">

    <xsl:output doctype-system="mypath.dtd" method="xml" encoding="UTF-8"/>

    <xsl:template match="test">
       <!-- stylesheet logic -->
    </xsl:template>

</xsl:stylesheet>

The above output, can then be used to transform your actual XML document.

The above preprocess transformation could be followed by your actual
transformation (the two transformations could be joined via a batch file or
a shell script), to produce the desired output.

On Wed, May 15, 2019 at 11:14 AM Trevor Nicholls 
trevor(_at_)castingthevoid(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi



I've got a simple stylesheet which is executed as a pre-process when XML
documents are loaded into FrameMaker. At the moment the stylesheet includes
the line



    <xsl:output doctype-system="/path/to/docs.dtd" method="xml"
encoding="UTF-8" />



Because this stylesheet will be used by different users in several
different contexts, it would be brilliant if the path to the DTD could be
parameterised. I know the following is invalid but the purpose of this post
is to ask if there is any way of engineering an equivalent:



    <xsl:param name="DefaultDTDPath" />

    <xsl:output doctype-system="{$DefaultDTDPath}" method="xml"
encoding="UTF-8" />



XSL version has to be 1.0 or 2.0.



Thanks

T





-- 
Regards,
Mukul Gandhi
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>