可以对动态生成的xml进行xsl转换吗?

时间:2009-06-24 20:45:01

标签: xml xslt dynamic

假设我有一个空的XML文件,如下所示:

<root></root>

我希望在XSL转换过程中向root添加一个元素,如下所示:

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

  <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:copy>
      <xsl:element name="root">
        <xsl:element name="label">Some text</xsl:element>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

转换XML文件给了我:

<root>
  <label>Some text</label>
</root>

但是,我想修改XSL文件,以便我可以转换运行时生成的XML。基本上,我希望XSL认为原始XML文件包含动态创建的XML,然后对其进行HTML转换。

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

  <xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:copy>
      <xsl:element name="root">
        <xsl:element name="label">Some text</xsl:element>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

  <!-- This part is new -->
  <xsl:template match="//label">
    <b>
      <xsl:value-of select="." />
    </b>
  </xsl:template>

</xsl:stylesheet>

在这种情况下,我想要的输出是:<b>label</b>

是否可以在一个XSL文件中执行此操作?我不正确地看待这个问题吗?

2 个答案:

答案 0 :(得分:3)

运行多遍变换有三个基本要素。

  • <xsl:import>
  • <xsl:apply-imports>
  • node-set()扩展功能

该示例通过两个串行转换传递XML文档。也就是说,它通过转换来传递内容,该转换删除命名空间节点,然后从第一个转换获取输出并将其传递给更改文档标题的第二个转换。在这种情况下,文档是XHTML文档。编写第二个转换,使其无法接受定义了命名空间的XHTML文档。

原始XHTML文档

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>This is the old title</title>
  </head>
  <body>
    <p>This is some text.</p>
  </body>
</html>

传递1后(删除命名空间节点)

<html>
  <head>
    <title>This is the old title</title>
  </head>
  <body>
    <p>This is some text.</p>
  </body>
</html>

结果(通过2之后)

请注意,标题文字已更改。

<html>
  <head> 
    <title>This is the new title</title>
  </head>
  <body>
    <p>This is some text.</p>
  </body>
</html>

传递1的XSLT

此转换将此文件中的模板应用于内容以删除命名空间节点,但复制其余内容。然后,在第2遍期间,使用<xsl:apply-imports>标记应用导入的XSLT中定义的模板。传递2的模板使用<xsl:import>标记导入。

第一遍的结果存储在名为“treefrag”的变量中。使用扩展函数“node-set()”将树片段转换为节点集。在此示例中,使用了Microsoft XML解析器4.0,因此声明了urn:schemas-microsoft-com:xslt命名空间。

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>

  <xsl:import href="pass2.xslt" />
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:template match="/">
    <xsl:param name="pass">1</xsl:param>

    <xsl:choose>
      <xsl:when test="$pass=1">
        <xsl:variable name="treefrag">
          <xsl:apply-templates>
            <xsl:with-param name="pass" select="$pass" />
          </xsl:apply-templates>
        </xsl:variable>
        <xsl:variable name="doc" select="
          msxsl:node-set($treefrag)
        " />
        <xsl:apply-templates select="$doc">
          <xsl:with-param name="pass">2</xsl:with-param>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="$pass=2">
        <xsl:apply-imports />
      </xsl:when>
    </xsl:choose>

  </xsl:template>   

  <!-- identity template without namespace nodes -->
  <xsl:template match="*">
    <xsl:param name="pass">2</xsl:param>

    <xsl:choose>
      <xsl:when test="$pass=1">
        <xsl:element name="{name()}">
          <xsl:apply-templates select="@*|node()">
            <xsl:with-param name="pass" select="$pass" />
          </xsl:apply-templates>
        </xsl:element>
      </xsl:when>
      <xsl:when test="$pass=2">
        <xsl:apply-imports />
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*|text()|comment()|processing-instruction()">
    <xsl:param name="pass">2</xsl:param>

    <xsl:choose>
      <xsl:when test="$pass=1">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()">
            <xsl:with-param name="pass" select="$pass" />
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:when>
      <xsl:when test="$pass=2">
        <xsl:apply-imports />
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

传递2的XSLT

此转换只是更改TITLE标记的内容,可以复制所有其余内容。

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

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="title">
    <title>This is the new title</title>
  </xsl:template>

  <!-- identity template -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet> 

来源文章:"XSLT: Multi-pass transforms"

答案 1 :(得分:0)

道格的回答在技术上是正确的,他让我走上寻找节点集扩展功能的正确道路(谢谢!)。唯一的区别是我使用Xalan-java处理器而不是微软处理器。这是我努力实现的一个工作(非常简单)的例子:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="xalan">
<xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes"/>

<xsl:template match="label">
    <b><xsl:value-of select="." /></b>
</xsl:template>

<xsl:template name="someTemplate">
    <root>
        <label>hey there</label>
    </root>
</xsl:template>

<xsl:template match="/">
    <xsl:variable name="gen">
        <xsl:call-template name="someTemplate" />
    </xsl:variable>

    <xsl:apply-templates select="xalan:nodeset($gen)//label" />
</xsl:template>

</xsl:stylesheet>

基本思想是创建一个调用模板(“someTemplate”)的变量(此处命名为“gen”),该模板创建一些xml。然后,xalan树由xalan:nodeset函数处理。在这里,我运行apply-templates并处理所有标签字段,这些字段与我的标签模板匹配并创建粗体标签。

我之所以这样做是因为我正在开发一个网络应用程序,其中每个页面上的所有内容都是用XML定义的,然后使用XSL进行处理。这很好用,直到你想用AJAX调用的结果填充页面的部分,因为那么你需要在返回的XML(麻烦和坏)中的javascript函数内构建新的html,或者通过AJAX XML响应传递服务器端的XSL转换,只需将xhtml结果插入页面即可。

在我的情况下,因为我用XML描述了每个网页,所以我创建了具有“类型”的简单通用元素。我的元素是“块”,这是任何内容块。块可以具有子节点,例如“按钮”(其是链接或按钮),“列表”(其是多个块),“图像”或“输入”。架构的细节是无关紧要的;我只想表明我的XML模式非常简单和通用。

另一方面,我正在检索刷新/构建页面的AJAX XML响应是由不使用我的架构的第三方创建的。相反,它们提供的每个服务都为每个唯一请求返回非常具体的XML。出于这个原因,我需要一种方法将他们非常具体的XML转换为我非常通用的XML,以便我可以使用一组XSL模板对其进行转换。

以下是我正在使用的一个xsl模板的示例,它转换了AJAX响应并将其特定的XML转换为我的通用XML。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="xalan">
<xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes"/>

<!-- base.xsl includes a generic template that matches all blocks and routes
        to the needed template based on the block type -->
<xsl:include href="base.xsl" />

<xsl:template name="templateForAJAXResponse">
    <block>
        <type>list</type>
        <list>
            <type>itemBrowser</type>
            <xsl:for-each select="/response/items/item">
                <block>
                    <type>button</type>
                    <button>
                        <type>imageButton</type>
                        <href>something.action</href>
                        <src><xsl:value-of select="src" /></src>
                        <title><xsl:value-of select="name" /></title>
                    </button>
                </block>
            </xsl:for-each>
        </list>
    </block>
</xsl:template>

<xsl:template match="/">
    <xsl:variable name="gen">
        <xsl:call-template name="templateForAJAXResponse" />
    </xsl:variable>

    <div>
        <xsl:apply-templates select="xalan:nodeset($gen)/block" />
    </div>
</xsl:template>

</xsl:stylesheet>