使用多个具有相同 select 过滤器的应用模板
using more than one apply-templates with the same select filter
我想使用应用模板将一些模板应用到我的 xml,但我似乎无法弄清楚如何为每个 "data type" 使用多个模板类型。
例如,使用此 xml:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item name='1'>
first
</item>
<item name='2'>
second
</item>
<item name='3'>
third
</item>
</items>
我使用以下 xslt 来获得我想要的输出:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:template match="items/item">
<xsl:value-of select='.'></xsl:value-of>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<font color="blue">
<xsl:apply-templates select="items/item[@name='1']"></xsl:apply-templates>
</font>
<font color="red">
<xsl:apply-templates select="items/item[@name='1']"></xsl:apply-templates>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
即:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body><font color="blue">
first
</font><font color="red">
first
</font></body>
</html>
第一项为蓝色,然后是同一项为红色。但是有了这个,我仍然得到了很多我很想移入模板 "items/item" 的剪切粘贴样板,但我不知道如何获得相同的模板 [=24] =] 两种颜色之一。有没有办法不用上面代码中的包装器来做到这一点?
这个问题很相似,但可能不是很清楚:
XSL apply more than one template
如果您在 xsl:template 中使用 "mode" 属性,您可以使用相同的 select 属性专门化模板:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:template match="items/item" mode="blue">
<font color="blue"><xsl:value-of select='.'></xsl:value-of></font>
</xsl:template>
<xsl:template match="items/item" mode="red">
<font color="red"><xsl:value-of select='.'></xsl:value-of></font>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="items/item[@name='1']" mode="blue"></xsl:apply-templates>
<xsl:apply-templates select="items/item[@name='1']" mode="red"></xsl:apply-templates>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这应该允许您将红色和蓝色字体包装移动到模板定义中,并允许您根据模式属性选择它们。
我想使用应用模板将一些模板应用到我的 xml,但我似乎无法弄清楚如何为每个 "data type" 使用多个模板类型。 例如,使用此 xml:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item name='1'>
first
</item>
<item name='2'>
second
</item>
<item name='3'>
third
</item>
</items>
我使用以下 xslt 来获得我想要的输出:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:template match="items/item">
<xsl:value-of select='.'></xsl:value-of>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<font color="blue">
<xsl:apply-templates select="items/item[@name='1']"></xsl:apply-templates>
</font>
<font color="red">
<xsl:apply-templates select="items/item[@name='1']"></xsl:apply-templates>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
即:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body><font color="blue">
first
</font><font color="red">
first
</font></body>
</html>
第一项为蓝色,然后是同一项为红色。但是有了这个,我仍然得到了很多我很想移入模板 "items/item" 的剪切粘贴样板,但我不知道如何获得相同的模板 [=24] =] 两种颜色之一。有没有办法不用上面代码中的包装器来做到这一点?
这个问题很相似,但可能不是很清楚:
XSL apply more than one template
如果您在 xsl:template 中使用 "mode" 属性,您可以使用相同的 select 属性专门化模板:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:template match="items/item" mode="blue">
<font color="blue"><xsl:value-of select='.'></xsl:value-of></font>
</xsl:template>
<xsl:template match="items/item" mode="red">
<font color="red"><xsl:value-of select='.'></xsl:value-of></font>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="items/item[@name='1']" mode="blue"></xsl:apply-templates>
<xsl:apply-templates select="items/item[@name='1']" mode="red"></xsl:apply-templates>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这应该允许您将红色和蓝色字体包装移动到模板定义中,并允许您根据模式属性选择它们。