使用xsl:copy-of时禁止默认属性

时间:2014-11-05 21:51:31

标签: xslt xslt-2.0

使用shape="rect"时,我无法弄清楚如何在xhtml输出文档中禁止默认属性xsl:copy-of

示例XML:

<div class="table">
<p class="table-caption"><span class="table-label">Table 7.2</span> Foo<a class="tabfn-ref"
        href="#s9781483390086.i825" id="s9781483390086.i808" shape="rect"><sup>a</sup></a> in
    Pricing</p>
<img alt="Table 11" class="table-image" src="10.4135_9781483390086-table11.jpg"/>
<table>
    <tgroup cols="3">
        <colspec align="left" colname="1" colnum="1"/>
        <colspec align="left" colname="2" colnum="2"/>
        <colspec align="left" colname="3" colnum="3"/>
        <thead>
            <row rowsep="1">
                <entry align="left">
                    <p/>
                </entry>
                <entry align="left">
                    <p>N</p>
                </entry>
                <entry align="left">
                    <p>%</p>
                </entry>
            </row>
        </thead>
        <tbody>
            <row>
                <entry>
                    <p>Foo<a class="tabfn-ref" href="#s9781483390086.i826"
                            id="s9781483390086.i809" shape="rect"><sup>b</sup></a></p>
                    <p>1–12</p>
                    <p>13–24</p>
                    <p>25–36</p>
                    <p>37–48</p>
                    <p>49–60</p>
                    <p>61–72</p>
                    <p>73–84</p>
                    <p>85–96</p>
                    <p>More than 96</p>
                </entry>
            </row>
            <row>
                <entry>
                    <p>Bar<a class="tabfn-ref" href="#s9781483390086.i827"
                            id="s9781483390086.i810" shape="rect"><sup>c</sup></a></p>
                    <p>1–20%</p>
                    <p>21–40%</p>
                    <p>41–60%</p>
                    <p>61–80%</p>
                    <p>More than 80%</p>
                </entry>
            </row>
        </tbody>
    </tgroup>
</table>
<div class="table-notes" id="s9781483390086.i825a">
    <p class="table-note" id="s9781483390086.i825b"><span class="label-fn"><a class="tabfn-ref"
                href="#s9781483390086.i808" id="s9781483390086.i825" shape="rect"
                ><sup>a</sup></a></span> Foo</p>
    <p class="table-note" id="s9781483390086.i825c"><span class="label-fn"><a class="tabfn-ref"
                href="#s9781483390086.i809" id="s9781483390086.i826" shape="rect"
                ><sup>b</sup></a></span> Bar</p>
    <p class="table-note" id="s9781483390086.i825d"><span class="label-fn"><a class="tabfn-ref"
                href="#s9781483390086.i810" id="s9781483390086.i827" shape="rect"
                ><sup>c</sup></a></span> Too</p>
</div>

示例XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xhtml="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xs xhtml" xmlns="http://www.w3.org/1999/xhtml"
    version="2.0">

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

    <!-- Suppress default shape attribute -->
    <xsl:template match="*/@shape"/>

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

    <xsl:template priority="1" match="//div[@class='table']//p[@class='table-caption']"/>

    <xsl:template match="table">
        <xsl:element name="table">
            <xsl:attribute name="hidden" select="'hidden'"/>
            <xsl:element name="caption">
                <xsl:copy-of select="preceding-sibling::p[@class='table-caption'] | *[not(preceding::a/@shape)]"/>
            </xsl:element>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>



</xsl:stylesheet>

我需要从输出中抑制shape="rect"。如您所见,除了使用xsl:copy-of的锚点外,所有锚点的输出都会被抑制。如何在行中指定抑制:

<xsl:copy-of select="preceding-sibling::p[@class='table-caption'] | *[not(preceding::a/@shape)]"/>

这显然不起作用。

输出:

    <div class="table">
   <img alt="Table 11" class="table-image" src="10.4135_9781483390086-table11.jpg"/>
   <table xmlns="http://www.w3.org/1999/xhtml" hidden="hidden">
      <caption>
         <p xmlns="" class="table-caption">
            <span class="table-label">Table 7.2</span> Foo <a class="tabfn-ref"
               href="#s9781483390086.i825" id="s9781483390086.i808" shape="rect">
               <sup>a</sup>
            </a> in Pricing </p>
      </caption>
      <tgroup xmlns="" cols="3">
         <colspec align="left" colname="1" colnum="1"/>
         <colspec align="left" colname="2" colnum="2"/>
         <colspec align="left" colname="3" colnum="3"/>
         <thead>
            <row rowsep="1">
               <entry align="left">
                  <p/>
               </entry>
               <entry align="left">
                  <p>N</p>
               </entry>
               <entry align="left">
                  <p>%</p>
               </entry>
            </row>
         </thead>
         <tbody>
            <row>
               <entry>
                  <p>Foo <a class="tabfn-ref" href="#s9781483390086.i826" id="s9781483390086.i809">
                        <sup>b</sup>
                     </a>
                  </p>
                  <p>1–12</p>
                  <p>13–24</p>
                  <p>25–36</p>
                  <p>37–48</p>
                  <p>49–60</p>
                  <p>61–72</p>
                  <p>73–84</p>
                  <p>85–96</p>
                  <p>More than 96</p>
               </entry>
            </row>
            <row>
               <entry>
                  <p>Bar <a class="tabfn-ref" href="#s9781483390086.i827" id="s9781483390086.i810">
                        <sup>c</sup>
                     </a>
                  </p>
                  <p>1–20%</p>
                  <p>21–40%</p>
                  <p>41–60%</p>
                  <p>61–80%</p>
                  <p>More than 80%</p>
               </entry>
            </row>
         </tbody>
      </tgroup>
   </table>
   <div class="table-notes" id="s9781483390086.i825a">
      <p class="table-note" id="s9781483390086.i825b">
         <span class="label-fn">
            <a class="tabfn-ref" href="#s9781483390086.i808" id="s9781483390086.i825">
               <sup>a</sup>
            </a>
         </span> Foo </p>
      <p class="table-note" id="s9781483390086.i825c">
         <span class="label-fn">
            <a class="tabfn-ref" href="#s9781483390086.i809" id="s9781483390086.i826">
               <sup>b</sup>
            </a>
         </span> Bar </p>
      <p class="table-note" id="s9781483390086.i825d">
         <span class="label-fn">
            <a class="tabfn-ref" href="#s9781483390086.i810" id="s9781483390086.i827">
               <sup>c</sup>
            </a>
         </span> Too </p>
   </div>
</div>

在第一个班级=&#34; tabfn-ref&#34;保留shape属性(由于copy-of)。我需要这个被压制。

1 个答案:

答案 0 :(得分:2)

使用xsl:copy-of时,您无法排除属性或节点。它执行所选节点的深层复制。

由于您已经使用带有空模板的标识转换来抑制@shape属性并且已经有一个匹配//div[@class='table']//p[@class='table-caption']的空模板,因此最简单的方法是更改​​{{1使用模式将<xsl:copy-of>添加到<xsl:apply-templates>并在该模式下定义模板以复制和应用模板。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xs xhtml" xmlns="http://www.w3.org/1999/xhtml"
    version="2.0">

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

    <!-- Suppress default shape attribute -->
    <xsl:template match="*/@shape"/>

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

    <xsl:template priority="1" 
                  match="//div[@class='table']//p[@class='table-caption']"/>

    <xsl:template match="//div[@class='table']//p[@class='table-caption']"
                  mode="caption">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="table">
        <table hidden="hidden">
            <caption>
                <xsl:apply-templates 
                               select="preceding-sibling::p[@class='table-caption'] 
                                     | *[not(preceding::a/@shape)]" mode="caption"/>
            </caption>
            <xsl:apply-templates/>
        </table>
    </xsl:template>

</xsl:stylesheet>
相关问题