English | Français | Deutsch | Magyar | >> 中文 << | Polski ZVON > Tutorials > XSLT Tutorial
>> 页 25 << | 上一条 | 下一条 | 目录 | 元素索引

xsl:copy 元素可以带一个 use-attribute-sets 属性。这样就可以同时复制节点的属性集。 XSLT stylesheet 2 并不像想象的那样工作( 在 name 函数中使用了 use-attribute-sets)。这是因为属性中的表达式还没有被求值。

XSLT stylesheet 1

XML源码
<source>

<h1>GREETING</h1>
<p>Hello, world!</p>

</source>

输出
<h1 align="center" style="color:red">GREETING</h1>
<p align="left" style="color:blue">Hello, world!</p>

用HTML察看

GREETING

Hello, world!

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

<xsl:template match="/">
     <xsl:apply-templates select="/source/*"/>
</xsl:template>

<xsl:template match="h1">
     <xsl:copy use-attribute-sets="H1">
          <xsl:value-of select="."/>
     </xsl:copy>
</xsl:template>

<xsl:template match="p">
     <xsl:copy use-attribute-sets="P ">
          <xsl:value-of select="."/>
     </xsl:copy>
</xsl:template>

<xsl:attribute-set name="H1">
     <xsl:attribute name="align">center</xsl:attribute>
     <xsl:attribute name="style">color:red</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="P">
     <xsl:attribute name="align">left</xsl:attribute>
     <xsl:attribute name="style">color:blue</xsl:attribute>
</xsl:attribute-set>

</xsl:stylesheet>


XSLT stylesheet 2

XML源码
<source>

<h1>GREETING</h1>
<p>Hello, world!</p>

</source>

输出
Failed to compile stylesheet. 1 error detected.
javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected.
at com.icl.saxon.PreparedStyleSheet.prepare(PreparedStyleSheet.java:136)
at com.icl.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:127)
at Main$TransformerThread.transform(Main.java:105)
at Main$TransformerThread.run(Main.java:80)
at java.lang.Thread.run(Thread.java:595)

用HTML察看
Failed to compile stylesheet. 1 error detected. javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected. at com.icl.saxon.PreparedStyleSheet.prepare(PreparedStyleSheet.java:136) at com.icl.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:127) at Main$TransformerThread.transform(Main.java:105) at Main$TransformerThread.run(Main.java:80) at java.lang.Thread.run(Thread.java:595)
XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
     <xsl:apply-templates select="/source/*"/>
</xsl:template>

<xsl:template match="*">
     <xsl:copy use-attribute-sets="{name(.)}">
          <xsl:value-of select="."/>
     </xsl:copy>
</xsl:template>

<xsl:attribute-set name="H1">
     <xsl:attribute name="align">center</xsl:attribute>
     <xsl:attribute name="style">color:red</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="P">
     <xsl:attribute name="align">left</xsl:attribute>
     <xsl:attribute name="style">color:blue</xsl:attribute>
</xsl:attribute-set>

</xsl:stylesheet>