Create XSL from XSL

2019-04-12 01:52发布

I am trying to dynamically generate an XSLT document from an XSLT stylesheet. In principle this works, of course, but I do not get the namespaces working. I want to have the generated XSLT elements to be prefixed with "xsl" prefix:

<xsl:stylesheet ...>

rather than

<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform">

I played around with namespace="" of xsl:element and with xsl:namespace but I do not get it working (xslt2/saxon available)

Any hints?

标签: xslt xslt-2.0
3条回答
够拽才男人
2楼-- · 2019-04-12 02:30

I found the solution:

<xsl:element name="xsl:stylesheet">
</xsl:element>

does the job! (i.e., avoid using namespace="" but explicitely list the namespace prefix)

查看更多
迷人小祖宗
3楼-- · 2019-04-12 02:34

The xsl:namespace-alias instruction was designed having exactly this use case in mind -- just start using it in your work.

Here is a real-world example:

http://dnovatchev.wordpress.com/2006/10/21/a-stylesheet-to-write-xslt-code/

查看更多
你好瞎i
4楼-- · 2019-04-12 02:45

If you want to use XSLT to create XSLT code then using http://www.w3.org/TR/xslt20/#element-namespace-alias helps e.g.

<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>
查看更多
登录 后发表回答