使用xslt显示xslt

时间:2012-05-02 11:25:38

标签: xslt

我想用一般的xslt显示我的xslt,以便生成有关如何构建xslt的文档。我在stackoverflow上得到了一些帮助,但是仍然有一些我认为自己设法做的细节,但不幸的是没有。

上一篇文章:Generate xsl documentation

这就是我希望它的工作方式:

我的xslt:

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

如果可能,Id还希望显示for-each循环和if语句。有没有人这样做,可以与我分享som代码?

1 个答案:

答案 0 :(得分:1)

我做了一些更改,我认为它可以帮助您,检查代码并在输入XSLT上运行它:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    <xsl:output method="text"/>

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

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

    <xsl:template match="xsl:value-of">
        <xsl:value-of select="@select" />
    </xsl:template>

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