【问题标题】:Is there a way to XSLT to echo out the XML powering it?XSLT 有没有办法回显支持它的 XML?
【发布时间】:2010-06-21 20:05:16
【问题描述】:

我正在使用 XSLT 来转换 XML。 XSLT 有没有办法吐出提供给它的 XML?比如:

<xsl:echo-xml />

【问题讨论】:

  • 把它吐到哪里,为了什么目的?在调试器中运行 XSL 怎么样?
  • 好问题 (+1)。请参阅我的回答,了解一些可能的解决方案,并详细解释。

标签: xml design-patterns xslt serialization reflection


【解决方案1】:

基本上我使用一些 XSLT 来 转换XML,有没有办法 XSLT 吐出 XML 是 喂食吗?比如:

最简单快捷的方法

<xsl:copy-of select="/"/>

这会输出当前的 XML 文档。

<xsl:copy-of select="."/>

这会输出以当前节点为根的子树。

但是,XSLT 程序员主要使用以下(身份规则):

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

当这是样式表中的唯一模板时,将应用转换的完整 XML 文档作为结果输出。

使用标识规则是最基本的 XSLT 设计模式之一。它使诸如复制所有节点但执行特定处理的特定节点(例如重命名删除,修改内容,...等)等任务非常容易/

【讨论】:

    【解决方案2】:

    以下将完整的 XML 复制到结果树:

    <xsl:copy-of select="." />
    

    如果你想把它发送到“消息输出”,你可以这样包装:

    <xsl:message>
        <xsl:copy-of select="."/>
    </xsl:message>
    

    【讨论】:

      【解决方案3】:

      使用带有name() XPath 函数和尖括号实体的模板来输出节点名称:

      <?xml version="1.0" encoding="utf-8"?>
      <?xml-stylesheet type="text/xsl" href="serialize.xml"?>
      <xsl:stylesheet version="1.0"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
                  >
      <xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />
      
      <xsl:template match="xsl:stylesheet">
        <xsl:apply-templates/>
      </xsl:template>
      
      <xsl:template match="/">
        <html>
          <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
          </head>
          <body>
            <xsl:apply-templates select="*" mode="serialize"/>
          </body>
        </html>
      </xsl:template>
      
          <xsl:template match="*" mode="serialize">
            &lt;<xsl:value-of select="name()" />&gt;
          <xsl:apply-templates select="*" mode="serialize"/>
      </xsl:template>
      </xsl:stylesheet>
      

      如需更多信息,Jeni Tennison 解释了 XSL-List 上 copying XML nodes to HTMLserializing external XML documents 的模板

      【讨论】:

        猜你喜欢
        • 2019-09-21
        • 1970-01-01
        • 2017-08-15
        • 1970-01-01
        • 2020-04-27
        • 2011-10-06
        • 2017-07-19
        • 2015-01-15
        • 1970-01-01
        相关资源
        最近更新 更多