开发者

Is there a way to XSLT to echo out the XML powering it?

I am u开发者_开发百科sing XSLT to transform XML. Is there a way for the XSLT to spit out the XML that is feeding it? Something like:

<xsl:echo-xml />


Basically I am using some XSLT to transform XML, is there a way for the XSLT to spit out the XML that is feeding it? Something like:

The easiest and shortest way:

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

This outputs the current XML document.

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

This outputs the subtree rooted by the current node.

However, XSLT programmers use mostly the following (identity rule):

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

When this is the only template in the stylesheet, the complete XML document on which the transformation is applied is output as result.

Using the identity rule is one of the most fundamental XSLT design patterns. It makes extremely easy such tasks as copying all nodes but specific ones for which a specific processing is performed (such as renaming deleting, modifying the contents, ..., etc)/


The following copies the full XML to the result tree:

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

If you want to send that to the "message output", you can just wrap this like that:

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


Use a template with the name() XPath function and angle bracket entities to output the node names:

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

For further information, Jeni Tennison explains templates for copying XML nodes to HTML as well as serializing external XML documents on XSL-List

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜