链接到XML

时间:2015-10-26 10:10:05

标签: xml xslt

大家好我想在我的XML代码中链接外部文件/链接,但它不起作用。你能告诉我正确的方法吗?

示例xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xml2html.xslt"?>
<Summary>
   <test name="test_2_4_4">
      <test name="tst_start_app">
        <result state="OK">
            <description 
                    href="test_45.xml">                     
                    <description >
                        <![CDATA[test_45.xml]]>
                    </description>
            </description>
        </result>
      </test>
   </test>
</Summary>

修改

这是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" version="5.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Summary/test">
    <html>
        <head>
            <style>

                #header {text-align:left;padding:5px;}

                #section {position:relative;left:20px;top:50px;}

                #nav {line-height:20px;width:300px;position:fixed;top: 140px;right: 5px;}

                #top {width:300px;position:fixed;top: 60px;right: 5px;}

                table, th, td {border: 1px solid black;border-collapse: collapse;} 

                th, td {padding:2px}

            </style>
        </head>
        <body>

            <div id="header">
                <h2> Squish Test Results Overview </h2>
            </div>

            <div id="section">
                <h3>Details</h3>                    
                <table>
                    <tr bgcolor="Peru">
                        <th>Testname</th>
                        <th>Testsuits</th>
                    </tr>
                    <xsl:for-each select="test">
                        <xsl:variable name="LinkName" select="attribute::name"/>
                        <xsl:variable name="LinkFile" select="descendant::node()/attribute::href"/>
                        <tr>
                            <th style="text-align:left;vertical-align:top;position:"><a name="{$LinkName}"><xsl:value-of select="$LinkName"/></a></th>
                            <xsl:for-each select="descendant::node()">
                                <xsl:choose>                                    
                                    <xsl:when test="attribute::state='NotOK'">
                                        <tr>
                                            <td bgcolor="red"><a href="#{$LinkFile}" title="click to open file"><xsl:value-of select="description"/></a></td>
                                        </tr>
                                    </xsl:when>
                                    <xsl:when test="attribute::state='OK'">
                                        <tr>
                                            <td bgcolor="lime"><a href="#{$LinkFile}" title="click to open file"><xsl:value-of select="description"/></a></td>
                                        </tr>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </table>
            </div>

            <div id="top">
                <a href="#" title="To the top of this page"><b>TO TOP</b>
                </a>
                <br/>
                <table>                     
                    <tr>
                        <th width="120" bgcolor="red"> Not OK </th>
                        <!-- <td>  Not OK  </td> -->
                    </tr>
                    <tr>
                        <th bgcolor="lime"> OK </th>
                        <!-- <td>  OK  </td> -->
                    </tr>
                </table>
            </div>              

            <div id="nav">
                <h3>Summary</h3>
                <table>
                    <tr bgcolor="coral">
                        <th>Test cases</th>
                        <th>More Info</th>
                    </tr>
                    <xsl:for-each select="test">
                        <xsl:variable name="LinkIt" select="@name"/>
                        <xsl:choose>
                            <xsl:when test="descendant::node()/@state='NotOK'">
                                <tr>
                                    <td bgcolor="red"><a href="#{$LinkIt}" title="click for Information"><xsl:value-of select="$LinkIt"/></a></td>
                                    <td>
                                        <xsl:value-of select="count(descendant::node()[@state='NotOK'])"/> of <xsl:value-of select="count(descendant::node()[@state='OK']) + count(descendant::node()[@state='NotOK'])"/> Not OK
                                    </td>                                       
                                </tr>
                            </xsl:when>
                            <xsl:when test="descendant::node()/attribute::state='OK'">
                                <tr>
                                    <td bgcolor="lime"><a href="#{$LinkIt}" title="click for Information"><xsl:value-of select="$LinkIt"/></a></td>
                                    <td>
                                        OK
                                    </td>

                                </tr>
                            </xsl:when>
                        </xsl:choose>                           
                    </xsl:for-each>
                </table>
            </div>          
        </body>
    </html>
</xsl:template>

我想要做的只是阅读xml文件并在普通浏览器中将其显示为html文件。

1 个答案:

答案 0 :(得分:0)

似乎您正在寻找XInclude

在你的情况下,它会看起来像ilke

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xml2html.xslt"?>
<Summary  xmlns:xi="http://www.w3.org/2001/XInclude">
   <test name="test_2_4_4">
      <test name="tst_start_app">
        <result state="OK">
            <xi:include href="test_45.xml" />
        </result>
      </test>
   </test>
</Summary>

test_45.xml内容

<description>
    <!--  The other content  -->
</description>