【问题标题】:Generate xsl documentation生成 xsl 文档
【发布时间】:2012-04-24 19:10:07
【问题描述】:

我想使用 php 为我的 xsl 生成可视化“文档”。我想要做的基本上是在没有 XML 的情况下转换我的 xsl,以便显示 XML 字段将如何在 HTML 中显示。

澄清一下:

xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/> 
    <xsl:template match="/">
        <head>
        <title>My sample</title>
    </head>
    <body>
        My sample element: <xsl:value-of select="root/element1"/>
    </body>
    </xsl:template>
</xsl:stylesheet>

请求的输出:

<html>
<head>
    <title>My sample</title>
</head>
<body>
    My sample element: root/element1
</body>
</html>

有人知道怎么做吗?

BR,杰克

【问题讨论】:

  • XSL 是一开始的语言家族。你在谈论 XSLT 吗?还请改写问题 - 一方面,我不了解您要实现的目标。
  • 请给出一些 XSL 的确切示例以及您想从中创建什么。 (我所说的“确切”不是指挥手的“类似的东西”,而是完整的描述。)
  • 请编辑问题并解释您在“xslt 文档”中的含义 - 这一点都不清楚。指定的想要的输出与 XSLT 文档无关——你确定你知道你想要什么吗?

标签: php xslt transform


【解决方案1】:

XSLT 是输入驱动的。如果将为不同的输入生成不同的输出。

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

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

<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>

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

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

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

【讨论】:

  • 谢谢。这就是我一直在寻找的东西。我一直在尝试添加一些我需要的额外功能,但没有成功。你能告诉我如何显示 if 语句和 for-each 循环吗?此外,我无法弄清楚如何在使用上述转换后将文本保留在 html 中。我已经编辑了我上面的例子来解释我想要做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
相关资源
最近更新 更多