我们可以在XSLT中添加自定义标记或函数。我将再次解释我在我的演示中添加了一个标记include-html
。当我们在样式表中找到include-html
匹配时,我们在XSLT中添加任何逻辑带有标记值的模板(与apply-template
中的相同)并获取值并显示在输出中。
这是我的代码。 http://xsltransform.net/6pS1zDt/1
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<hmtl>
<head>
<title>New Version!</title>
</head>
<include-html>a</include-html>
</hmtl>
</xsl:template>
<xsl:template match="a">
<xsl:variable name="ab" select="'ss'"/>
<p><xsl:value-of select="$ab"/></p>
</xsl:template>
</xsl:transform>
在我的示例中,我正在写a
值为include-html
的值。它与模板匹配并返回**<p>ss</p>**
<include-html>a</include-html>
预期输出
**<p>ss</p>**
答案 0 :(得分:0)
您可以使用某种元样式表(让我们将其命名为test.xslt
)来实现此目的:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/whatever">
<hmtl>
<head>
<title>New Version!</title>
</head>
<include-html>a</include-html>
</hmtl>
</xsl:template>
<xsl:template match="include-html[text()='a']">
<xsl:variable name="ab" select="'ss'"/>
<p><xsl:value-of select="$ab"/></p>
</xsl:template>
</xsl:transform>
如果您将此传递给XSLT处理器,如XSLT 和 XML输入,如
xsl.sh test.xslt test.xslt
输出
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml"
omit-xml-declaration="yes"
encoding="UTF-8"
indent="yes"/>
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/whatever"> <!-- must NOT match -->
<hmtl>
<head>
<title>New Version!</title>
</head>
<p>ss</p> <!-- REPLACED! -->
</hmtl>
</xsl:template>
<xsl:template match="include-html[text()='a']">
<xsl:variable name="ab" select="'ss'"/>
<p>
<xsl:value-of select="$ab"/>
</p>
</xsl:template>
</xsl:transform>
这接近你想要的,我想......