XSL最全基础教程之xsl:apply-imports元素

2025-11-07 15:11:10

1、<xsl:apply-imports>元素

    <xsl:apply-imports>元素导入其他样式表的模板捧菊规则,用该元素导入的模板规则可以用来重写主样式表中的模板规则。

    通过该元素导入的模板规则的优先级根据导入的顺率兼序由低到高,但都低于主样式表的模板规则。

2、语法

    <xsl:apply-imports/>

3、示例

创建一个名为operation.xml文件,该文件有三组操作数分别用来加、减、乘运算。

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="operation.xsl"?>

<operations>

      <operation name="add" symbol="+">

          <operand>1</operand>

          <operand>2</operand>

      </operation>

      <operation name="sub" symbol="-">

          <operand>1</operand>

          <operand>2</operand>

      </operation>

     <operation name="mul" symbol="*">

         <operand>1</operand>

         <operand>2</operand>

      </operation>

</operations>

XSL最全基础教程之xsl:apply-imports元素

4、创建xsl文件

创建名为operation.xsl文件,测试apply-imports元素。

<?xml version="1.0" encoding="UTF-8"?>

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

      <xsl:import href="arith.xsl"/>

      <xsl:import href="result.xsl"/>

      <xsl:template match="operations">

           <html>

                 <head>

                        <title>测试apply-imports元素</title>

                 </head>

                 <body>

                          <xsl:apply-templates select="operation"/>

                  </body>

            </html>

       </xsl:template>

       <xsl:template match="operation">

           <xsl:value-of select="operand[1]"/>

           <xsl:value-of select="@symbol"/>

           <xsl:value-of select="operand[2]"/>

           =<xsl:apply-imports/><br/>

       </xsl:template>

</xsl:stylesheet>

XSL最全基础教程之xsl:apply-imports元素

5、创建名为arith.xsl文件

导入arith.xsl文件,用于操作数运算

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:template match="operation[@symbol='+']">

<xsl:value-of select="sum(operand)"/>结果来自arith.xsl文

</xsl:template>

<xsl:template match="operation[@symbol='-']">倘酱没

<xsl:value-of select="number(operand[1])-number(operand[2])"/>

结果来自arith.xsl文件

</xsl:template>

<xsl:template match="operation[@symblo='*']">

<xsl:value-of select="number(operand[1])*number(operand[2])"/>

结果来自arith.xsl文件

</xsl:template>

</xsl:stylesheet>

XSL最全基础教程之xsl:apply-imports元素

6、创建名为result.xsl文件

导入result.xsl文件,用于操作数拼接

<?xml version="1.0" encoding="UTF-8"?>

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

     <xsl:template match="operation[@name='add']">

           <xsl:value-of select="operand[1]"/>

           <xsl:value-of select="operand[2]"/>

           运算结果来自result.xsl文件

      </xsl:template>

      <xsl:template match="operation[@name='mul']">

           <xsl:value-of select="operand[1]"/>

           <xsl:value-of select="operand[2]"/>

           运算结果来自result.xsl文件

      </xsl:template>

</xsl:stylesheet>

XSL最全基础教程之xsl:apply-imports元素

7、运算结果

用Firefox打开operation.xml文件,查看转换结果。

分析:从运行结果可以看出,后导入的result.xsl文件优先级别较arith.xsl文件,result.xsl的结果覆盖了arith.xsl的结果。

    由于result.xsl文件中没有加法运算,所以使用了arith.xsl的结果

XSL最全基础教程之xsl:apply-imports元素

1、<xsl:apply-imports>元素

   导入的文件可以为主样式表模板规则添加功能,但又不取代主样式表模板规则

2、创建xml文件

创建一个名为books.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="whole-style.xsl"?>

<books>

    <book>

        <name>Thinking in Java</name>

        <author>Bruce Eckel </author>

        <publisher>Prentice Hall</publisher>

        <publishyear>2006-2-20</publishyear>

        <pages>1150</pages>

<price>60.0</price>

    </book>

    <book>

        <name>Effective Java</name>

        <author>Joshua Bloch</author>

        <publisher>Addison-Wesley Professional</publisher>

        <publishyear>2001-6-05</publishyear>

        <pages>272</pages>

<price>38.9</price>

    </book>

    <book>

        <name>Head First Java</name>

        <author>Elisabeth Freeman</author>

        <publisher>O'Reilly Media</publisher>

        <publishyear>2004-11-1</publishyear>

        <pages>678</pages>

<price>41.0</price>

    </book>

</books>

XSL最全基础教程之xsl:apply-imports元素

3、创建xsl文件

创建一个名为whole-style.xsl文件

<?xml version="1.0" encoding="UTF-8"?>

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

       <xsl:import href="import-style.xsl"/>

       <xsl:template match="book">

           <font face="Arial">

                <xsl:apply-imports/>

           </font>

       </xsl:template>

</xsl:stylesheet>

XSL最全基础教程之xsl:apply-imports元素

4、创建导入xsl文件

创建一个名为import-style.xsl文件

<?xml version="1.0" encoding="UTF-8"?>

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

      <xsl:template match="text()"/>

      <xsl:template match="/">

           <html>

                 <body>

                       <xsl:apply-templates />

                 </body>

           </html>

        </xsl:template>

        <xsl:template match="book">

             <i>

                   <xsl:apply-templates select="name"/>

             </i>

             <xsl:text>By:</xsl:text>

             <xsl:apply-templates select="author"/>

          </xsl:template>

          <xsl:template match="name">

              <b>

                   <xsl:value-of select="."/>

              </b>

           </xsl:template>

          <xsl:template match="author">

                <font color="blue">

                        <xsl:value-of select="."/><br/>

                </font>

           </xsl:template>

</xsl:stylesheet>

XSL最全基础教程之xsl:apply-imports元素

5、运行结果

用Firefox打开books.xml文件

XSL最全基础教程之xsl:apply-imports元素

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢