【问题标题】:XSL Transform to output a number of embedded XSL stylesheetsXSL 转换以输出许多嵌入式 XSL 样式表
【发布时间】:2021-01-27 05:04:31
【问题描述】:

我希望编写一个 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 部分,但我不想这样做。

桑杰

【问题讨论】:

    标签: xml xslt xml-parsing


    【解决方案1】:

    您似乎想为结果元素使用命名空间别名 https://www.w3.org/TR/xslt-30/#element-namespace-alias,例如

     <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
    

    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>
    

    【讨论】:

    • 非常感谢马丁。我认为这将解决我的问题。
    猜你喜欢
    • 2012-04-03
    • 2014-12-20
    • 2012-04-01
    • 2017-08-22
    • 1970-01-01
    • 2013-09-06
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多