XSL Transform 输出大量嵌入式 XSL 样式表
XSL Transform to output a number of embedded XSL stylesheets
我希望编写一个 XSL 转换,在 TEMPLATE 元素内输出许多嵌入的样式表(样式表集合由另一个组件在下游处理,该组件提取所需的样式表并应用它)。因此,作为转换的结果,我想要生成的是一个 XML 文件,其中包含如下内容:
structure of the desired XML
<?xml version="1.0"?>
<TEMPLATEDATA xmlns="http://www.sanjay.com/appname" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<TEMPLATE name="addFocus" operation="Add">
<xsl:stylesheet version="1.0">
<xsl:template match="/">
<STRATEGICFOCUS />
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
<TEMPLATE name="addOrg" operation="Init">
<xsl:stylesheet version="1.0" xmlns:app="http://www.sanjay.com/myapp">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="Name"></xsl:param>
<xsl:template match="*">
<CONTENT>
<NAME><xsl:value-of select="$Name"/></NAME>
</CONTENT>
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
</TEMPLATEDATA>
我想通过像这样定义 XSL 转换来生成此输出(忽略我正在转换的输入 XML,因为它不重要):
desired transform
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.sanjay.com/myapp">
<xsl:template match="/">
<TEMPLATEDATA>
<xsl:call-template name="template1" />
<xsl:call-template name="template2" />
</TEMPLATEDATA>
</xsl:template>
<xsl:template name="template1">
<TEMPLATE name="addFocus" operation="Add">
<xsl:stylesheet version="1.0">
<xsl:template match="/">
<STRATEGICFOCUS />
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
</xsl:template>
<xsl:template name="template2">
<TEMPLATE name="addOrg" operation="Init"
<xsl:stylesheet version="1.0" xmlns:app="http://www.sanjay.com/myapp">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="Name"></xsl:param>
<xsl:template match="*">
<CONTENT>
<NAME>
<xsl:value-of select="$Name"/>
</NAME>
</CONTENT>
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
</xsl:template>
</xsl:stylesheet>
我收到样式表的 XSL 解析错误,指出内部 xsl:stylesheet 无效,因为它不能是 TEMPLATE 元素的子元素。谁能告诉我我必须做些什么才能解决这个问题?我知道我可以将内部样式表嵌入 CDATA 部分,但我不想那样做。
桑杰
您似乎想为结果元素使用命名空间别名 https://www.w3.org/TR/xslt-30/#element-namespace-alias,例如
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
with xmlns:axsl="http://www.w3.org/1999/XSL/Transform-alias"
声明并用于例如
<xsl:template name="template1">
<TEMPLATE name="addFocus" operation="Add">
<axsl:stylesheet version="1.0">
<axsl:template match="/">
<STRATEGICFOCUS />
</axsl:template>
</xasl:stylesheet>
</TEMPLATE>
</xsl:template>
我希望编写一个 XSL 转换,在 TEMPLATE 元素内输出许多嵌入的样式表(样式表集合由另一个组件在下游处理,该组件提取所需的样式表并应用它)。因此,作为转换的结果,我想要生成的是一个 XML 文件,其中包含如下内容:
structure of the desired XML
<?xml version="1.0"?>
<TEMPLATEDATA xmlns="http://www.sanjay.com/appname" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<TEMPLATE name="addFocus" operation="Add">
<xsl:stylesheet version="1.0">
<xsl:template match="/">
<STRATEGICFOCUS />
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
<TEMPLATE name="addOrg" operation="Init">
<xsl:stylesheet version="1.0" xmlns:app="http://www.sanjay.com/myapp">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="Name"></xsl:param>
<xsl:template match="*">
<CONTENT>
<NAME><xsl:value-of select="$Name"/></NAME>
</CONTENT>
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
</TEMPLATEDATA>
我想通过像这样定义 XSL 转换来生成此输出(忽略我正在转换的输入 XML,因为它不重要):
desired transform
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.sanjay.com/myapp">
<xsl:template match="/">
<TEMPLATEDATA>
<xsl:call-template name="template1" />
<xsl:call-template name="template2" />
</TEMPLATEDATA>
</xsl:template>
<xsl:template name="template1">
<TEMPLATE name="addFocus" operation="Add">
<xsl:stylesheet version="1.0">
<xsl:template match="/">
<STRATEGICFOCUS />
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
</xsl:template>
<xsl:template name="template2">
<TEMPLATE name="addOrg" operation="Init"
<xsl:stylesheet version="1.0" xmlns:app="http://www.sanjay.com/myapp">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="Name"></xsl:param>
<xsl:template match="*">
<CONTENT>
<NAME>
<xsl:value-of select="$Name"/>
</NAME>
</CONTENT>
</xsl:template>
</xsl:stylesheet>
</TEMPLATE>
</xsl:template>
</xsl:stylesheet>
我收到样式表的 XSL 解析错误,指出内部 xsl:stylesheet 无效,因为它不能是 TEMPLATE 元素的子元素。谁能告诉我我必须做些什么才能解决这个问题?我知道我可以将内部样式表嵌入 CDATA 部分,但我不想那样做。
桑杰
您似乎想为结果元素使用命名空间别名 https://www.w3.org/TR/xslt-30/#element-namespace-alias,例如
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
with xmlns:axsl="http://www.w3.org/1999/XSL/Transform-alias"
声明并用于例如
<xsl:template name="template1">
<TEMPLATE name="addFocus" operation="Add">
<axsl:stylesheet version="1.0">
<axsl:template match="/">
<STRATEGICFOCUS />
</axsl:template>
</xasl:stylesheet>
</TEMPLATE>
</xsl:template>