且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

生成 xsl 文档

更新时间:2023-02-15 16:07:21

XSLT 是输入驱动的.如果会为不同的输入生成不同的输出.

XSLT is input-driven. If will generate different output for different input.

在任何比您的简单示例更复杂的实际场景中,在没有任何输入的情况下查看代码意味着您无法说出输出会是什么样子.

In any real-world scenario that is more complex than your simple example, looking at the code without any input to run through it means you can't say what the output is going to look like.

对于您的简单示例,您可以通过另一个 XSLT 样式表运行您的 XSLT 样式表.

For your simple example, you could run your XSLT stylesheet thorugh another XSLT stylesheet.

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="text" />

  <xsl:template match="*">
    <xsl:value-of select="concat('&lt;', name())" />
    <xsl:apply-templates select="@*" />
    <xsl:value-of select="'&gt;'" />
    <xsl:apply-templates select="*" />
    <xsl:value-of select="concat('&lt;/', name(), '&gt;')" />
  </xsl:template>

  <xsl:template match="@*">
    <xsl:value-of select="concat(' ', name(), '=&quot;', ., '&quot;')" />
  </xsl:template>

  <xsl:template match="xsl:*">
    <xsl:apply-templates select="*" />
  </xsl:template>

  <xsl:template match="xsl:value-of">
    <xsl:value-of select="concat('{{value-of: ', @select, '}}')" />
  </xsl:template>

  <!-- add appropriate templates for the other XSLT elements -->
</xsl:stylesheet>

使用您的示例,这会生成字符串

With your sample, this produces the string

<head><title></title></head><body>{{value-of: root/element1}}</body>

然而,为其他 XSLT 元素添加适当的模板" 部分是困难的部分.您的输出将按照输入的顺序排列(正如我所说,XSLT 是输入驱动的).您的 XSLT 程序很可能不会以与其将要生成的输出相同的方式进行布局,因此从中生成合理的文档可能比您想象的要困难得多.

However, the "add appropriate templates for the other XSLT elements" part is the difficult bit. Your output will be in the order of the input (XSLT is input-driven, as I said). Your XSLT program will most likely not be layed out the same way as the output it is going to produce, so generating sensible documentation from it might be quite a bit harder than you think.