XSLT:在运行时将xmlns:prefix属性添加到别名命名空间中的样式表

时间:2016-11-26 19:17:47

标签: xslt

我有一个XSLT样式表,可以创建另一个XSLT样式表。由第一个创建的输出XSLT需要具有将在运行时设置的xmlns前缀/命名空间定义。下面是我尝试过的一个例子,但它没有用。我不知道如何在使用params时在alias-xsl:stylesheet上设置xmlns:$ somePrefix = $ someNamespace。关于如何做到这一点的任何想法?我还添加了一个所需输出的示例。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:alias-xsl="http://www.w3.org/1999/XSL/TransformAlias" version="2.0">
  <xsl:param name="someNamespace"/>
  <xsl:param name="somePrefix"/>
  <xsl:namespace-alias result-prefix="xsl" stylesheet-prefix="alias-xsl"/>
  <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <alias-xsl:stylesheet version="2.0">
      <xsl:attribute name="{$somePrefix}">
        <xsl:value-of select="$someNamespace"/>
      </xsl:attribute>
      <alias-xsl:output method="xml"/>
      <alias-xsl:template match="/">
        <some-output>
          <alias-xsl:apply-templates/>
        </some-output>
      </alias-xsl:template>
      <xsl:apply-templates/>
    </alias-xsl:stylesheet>
  </xsl:template>
</xsl:stylesheet>

所需输出的示例($ somePrefix = xmlns:s和$ someNamespace = http://example.com):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
      xmlns:s="http://example.com">
  <xsl:output method="xml"/>
  <xsl:template match="/">
    <some-output>
      <xsl:apply-templates/>
    </some-output>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

以下样式表生成所需的输出(在http://www.freeformatter.com/xsl-transformer.html上试用)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:alias-xsl="http://www.w3.org/1999/XSL/TransformAlias"
  version="2.0">
  <xsl:param name="someNamespace">http://example.com</xsl:param>
  <xsl:param name="somePrefix">s</xsl:param>
  <xsl:namespace-alias result-prefix="xsl" stylesheet-prefix="alias-xsl"/>
  <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <alias-xsl:stylesheet version="2.0">

      <xsl:namespace name="{$somePrefix}" select="$someNamespace"/>

      <alias-xsl:output method="xml"/>
      <alias-xsl:template match="/">
        <some-output>
          <alias-xsl:apply-templates/>
        </some-output>
      </alias-xsl:template>
      <xsl:apply-templates/>
    </alias-xsl:stylesheet>
  </xsl:template>
</xsl:stylesheet>

您可能需要查看http://www.xmlplease.com/xsl-namespace,其中说明了已添加的xsl:namespace行。请注意,我将somePrefix参数值指定为s而不是xmlns:s