Los*_*der 2 xml xslt templates apply-templates
XML:
<Root>
<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>
<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>
</Root>
Run Code Online (Sandbox Code Playgroud)
尝试生成为同一元素应用两个不同的模板.
主要模板:
<xsl:stylesheet version="1.0">
<xsl:template match="/Root">
At root level
<xsl:apply-templates select="Elements">
<h1>Render something more</h1>
<xsl:apply-templates select="Elements" mode="1:Custom">
</xsl:template>
<!-- This doesn't render though it is called above-->
<xsl:template match="Elements">
render something here
</xsl:template>
<!-- This renders twice -->
<xsl:template match="Elements" mode="1:Custom">
render something else here
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
如果我将模式添加到第一个模板,则两者都不会渲染.
还尝试过:
<xsl:apply-templates select="Elements" mode="1:Custom" />
Run Code Online (Sandbox Code Playgroud)
使用不同的模板应用为:
<xsl:apply-templates select="Elements" mode="Different" />
Run Code Online (Sandbox Code Playgroud)
只有两个中的一个(第一个具有指定模式的渲染).即
<xsl:template match="Elements">
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
没有呈现
或
<xsl:template match="Elements" mode="Different" />渲染两次.
我该怎么解决这个问题?我研究过的每个地方都建议优先考虑模式.这么多程序员使用它必须是简单的东西吗?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Root">
At root level
<xsl:apply-templates select="Elements"/>
<h1>After first template</h1>
<xsl:apply-templates select="Elements" mode="Custom"/>
</xsl:template>
<xsl:template match="Elements">
<p>First template</p>
<xsl:apply-templates select="Element"/>
</xsl:template>
<xsl:template match="Elements" mode="Custom">
<p>Second template </p>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)<xsl:template match="Elements" mode="1:Custom">
您在此处使用了语法上非法的模式名称(必须是QName)并且任何兼容的 XSLT 处理器都必须发出错误。
解决方案:只需更改
mode="1:Custom"
Run Code Online (Sandbox Code Playgroud)
到
mode="Custom"
Run Code Online (Sandbox Code Playgroud)
因此,这种转换是正确的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Root">
At root level
<xsl:apply-templates select="Elements"/>
<h1>Render something more</h1>
<xsl:apply-templates select="Elements" mode="Custom"/>
</xsl:template>
<xsl:template match="Elements">
render something here
</xsl:template>
<xsl:template match="Elements" mode="Custom">
render something else here
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的 XML 文档时:
<Root>
<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>
<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>
</Root>
Run Code Online (Sandbox Code Playgroud)
产生了想要的、正确的结果:
At root level
render something here
render something here
<h1>Render something more</h1>
render something else here
render something else here
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4022 次 |
| 最近记录: |