我正在尝试从 XSLT 样式表动态生成 XSLT 文档。当然,原则上这是可行的,但我没有让命名空间正常工作。我希望生成的 XSLT 元素带有“xsl”前缀:
<xsl:stylesheet ...>
而不是
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform">
我玩过 xsl:element 的 namespace="" 和 xsl:namespace 但我没有让它工作(xslt2/saxon 可用)
有什么提示吗?
如果您想使用 XSLT 创建 XSLT 代码,那么使用http://www.w3.org/TR/xslt20/#element-namespace-alias会有所帮助,例如
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:axsl="file://namespace.alias">
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:template match="/">
<axsl:stylesheet version="2.0">
<xsl:apply-templates/>
</axsl:stylesheet>
</xsl:template>
<xsl:template match="elements">
<axsl:template match="/">
<axsl:comment select="system-property('xsl:version')"/>
<axsl:apply-templates/>
</axsl:template>
</xsl:template>
<xsl:template match="block">
<axsl:template match="{.}">
<fo:block><axsl:apply-templates/></fo:block>
</axsl:template>
</xsl:template>
</xsl:stylesheet>
我找到了解决方案:
<xsl:element name="xsl:stylesheet">
</xsl:element>
做这项工作!(即,避免使用 namespace="" 但明确列出命名空间前缀)
该xsl:namespace-alias说明的设计正是考虑到了这个用例——只需开始在您的工作中使用它。
这是一个真实的例子:
http://dnovatchev.wordpress.com/2006/10/21/a-stylesheet-to-write-xslt-code/