Unanticipated <xsl:apply-imports/> behavior

人走茶凉 提交于 2019-12-24 04:30:14

问题


I've been trying to figure out how to best modularize my XSLT stylesheets to facilitate re-use. I hit upon the idea of using <xsl:apply-imports/> as a way of introducing document-specific attributes to standard tag transformations. This is not working the way I expected it would, and I can't even begin to fathom what is going on here. Here is a simplified version of the stylesheet:

<!-- main.xsl -->
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet  version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:import href="html-customizations.xsl"/>

<xsl:output method="xml"
   indent="yes"
   omit-xml-declaration="no"/>

<xsl:template match="para">
  <fo:block>
      <xsl:attribute name="space-after">1em</xsl:attribute>
      <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<!-- =============== -->
<!-- Inline Elements -->
<!-- =============== -->

<xsl:template match="i">
  <fo:inline font-style="italic">
    <xsl:apply-imports/>
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<!-- ================ -->
<!--      Tables      -->
<!-- ================ -->

<xsl:template match="table">
  <fo:table>
    <xsl:apply-imports/>
    <xsl:apply-templates/>
  </fo:table>
</xsl:template>

<xsl:template  match="tr">
  <fo:table-row>
    <xsl:apply-imports/>
    <xsl:apply-templates/>
  </fo:table-row>
</xsl:template>

<xsl:template match="td | th">
  <fo:table-cell>
    <xsl:apply-imports/>
    <xsl:apply-templates/>
  </fo:table-cell>
</xsl:template>
</xsl:stylesheet>

The imported stylesheet:

<!-- html-customizations.xsl -->
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet  version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template  match="td | th">
  <xsl:attribute name="hyphenate">true</xsl:attribute>
</xsl:template>

</xsl:stylesheet>

Here is the XML input file:

<!-- test.xml -->
<para>
  <table>
    <tr><td>Spongebob Squarepants, <i>Chair</i></td></tr>
    <tr><td>Patrick Starfish, <i>Vice Cchair</i></td></tr>
    <tr><td>Squidword, <i>Secretary</i></td></tr>
  </table>
</para>

$ xalan -o out.xml test.xml main.xsl

out.xml:
<?xml version="1.0" encoding="UTF-8"?>
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format" space-after="1em">
  <fo:table>
    <fo:table-row>
<fo:table-cell hyphenate="true">Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>
</fo:table-cell>
<fo:table-cell hyphenate="true">Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>
</fo:table-cell>
</fo:table-row>
    <fo:table-row>
<fo:table-cell hyphenate="true">Patrick Starfish, <fo:inline font-style="italic">Vice CchairVice Cchair</fo:inline>
</fo:table-cell>
<fo:table-cell hyphenate="true">Patrick Starfish, <fo:inline font-style="italic">Vice CchairVice Cchair</fo:inline>
</fo:table-cell>
</fo:table-row>
    <fo:table-row>
<fo:table-cell hyphenate="true">Squidword, <fo:inline font-style="italic">SecretarySecretary</fo:inline>
</fo:table-cell>
<fo:table-cell hyphenate="true">Squidword, <fo:inline font-style="italic">SecretarySecretary</fo:inline>
</fo:table-cell>
</fo:table-row>

    <fo:table-row>
<fo:table-cell hyphenate="true">Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>
...
...

As you can see, every child of an element matched by a template that includes <xsl:apply-imports/> is repeated! I included the imported stylesheet in order to illustrate what I'm trying to do. If I comment out this import:

<!--
<xsl:import href="html-customizations.xsl"/>
-->

The repeating behavior is the same:

<?xml version="1.0" encoding="UTF-8"?>
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format" space-after="1em">
  <fo:table>
    <fo:table-row>
<fo:table-cell>Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>
</fo:table-cell>
<fo:table-cell>Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>
</fo:table-cell>
</fo:table-row>
    <fo:table-row>
...
...

sans the attribute I'm trying to add from the imported stylesheet; i.e. just the presence of the <xsl:apply-imports/> processing instruction causes the output elements to be doubled. Also note that this is not just a xalan problem -- the same thing happens on MSXML on Windows 7.

Any thoughts? I was counting on this working, so am now pulling my hair out trying to figure out how to fix this so it works.

BTW, my assumptions of how <xsl:apply-imports/> can be used is based on the examples given under the xsl:import section of Michael Kay's book. If anyone knows of a reference that explains the behavior I'm seeing above, please share.


回答1:


I agree that the behaviour of apply-imports is difficult to understand. The problem is that apply-imports always finds a template that matches the current node, even if the user did not define it. In that case, the default template applies.

The following stylesheet works:

XSLT Stylesheet

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

    <xsl:import href="html-customizations.xsl"/>

    <xsl:output method="xml"
        indent="yes"
        omit-xml-declaration="no"/>

    <xsl:template match="para">
        <fo:block>
            <xsl:attribute name="space-after">1em</xsl:attribute>
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>

    <!-- =============== -->
    <!-- Inline Elements -->
    <!-- =============== -->

    <xsl:template match="i">
        <fo:inline font-style="italic">
            <xsl:apply-templates/>
        </fo:inline>
    </xsl:template>

    <!-- ================ -->
    <!--      Tables      -->
    <!-- ================ -->

    <xsl:template match="table">
        <fo:table>

            <xsl:apply-templates/>
        </fo:table>
    </xsl:template>

    <xsl:template  match="tr">
        <fo:table-row>

            <xsl:apply-templates/>
        </fo:table-row>
    </xsl:template>

    <xsl:template match="td | th">
        <fo:table-cell>
            <xsl:apply-imports/>
            <xsl:apply-templates/>
        </fo:table-cell>
    </xsl:template>
</xsl:stylesheet>

As you can see, I have removed two apply-imports elements, only leaving the one inside template/@match='td | th'. Then, the output will be

XML Output

<?xml version="1.0" encoding="UTF-8"?>
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format" space-after="1em">
    <fo:table>
        <fo:table-row>
         <fo:table-cell hyphenate="true">Spongebob Squarepants, <fo:inline font-style="italic">ChairChair</fo:inline>
         </fo:table-cell>
      </fo:table-row>
        <fo:table-row>
         <fo:table-cell hyphenate="true">Patrick Starfish, <fo:inline font-style="italic">Vice CchairVice Cchair</fo:inline>
         </fo:table-cell>
      </fo:table-row>
        <fo:table-row>
         <fo:table-cell hyphenate="true">Squidword, <fo:inline font-style="italic">SecretarySecretary</fo:inline>
         </fo:table-cell>
      </fo:table-row>
    </fo:table>
</fo:block>

What exactly is happening?

apply-imports looks for a template that

  • matches the current node
  • matches the current mode
  • is inside an imported stylesheet

Now, the crucial bit is: this instruction will invoke the built-in templates if no such template can be found in an imported stylesheet. In the case of tr:

<xsl:template  match="tr">
  <fo:table-row>
    <xsl:apply-imports/>
    <xsl:apply-templates/>
  </fo:table-row>
</xsl:template>

The default action for element nodes is traversing it and applying templates to its content, so the snippet above actually translates to

<xsl:template  match="tr">
  <fo:table-row>
    <xsl:apply-templates/>
    <xsl:apply-templates/>
  </fo:table-row>
</xsl:template>

and this is why the output contains duplicates. I assume now you also understand why commenting out xsl:import did not help, otherwise I'm glad to elaborate.


Since you were also asking for a reference, this is explained in the XSLT 2.0 and XPath 2.0 Programmer's Reference by Michael Kay, page 238.



来源:https://stackoverflow.com/questions/29943838/unanticipated-xslapply-imports-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!