当xml的节点中存在“xmlns”属性时,xsl将如何工作

时间:2010-10-19 14:06:35

标签: xslt

当xml的node_test_results节点中不存在“xmlns”属性时,我的xsl工作正常。我可以在xsl中做什么,以便在integration_test_results节点中存在“xmlns”属性时它将起作用。

请尽快帮助我。

这里我附加了我的xml和xsl文件:

附加xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet href="framework_results.xsl" type="text/xsl" ?>
<integration_test_results xmlns="http://schemas.oracle.com/dm/v2009">
    <test>
        <name>Reg_Table_test_1</name>
        <status>PASSED</status>
        <start_time>2010-10-19 05:04:58.011</start_time>
        <finish_time>2010-10-19 05:07:29.779</finish_time>
        <test_duration>0</test_duration>
        <datamover_job>
            <status>COMPLETED_SUCCESSFUL</status>
            <start_time>2010-10-19 05:04:58.011</start_time>
            <finish_time>2010-10-19 05:07:29.779</finish_time>
            <job_duration>0</job_duration>
        </datamover_job>
    </test>
</integration_test_results>

附加xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:ns="http://schemas.oracle.com/dm/v2009">

 <xsl:template match="ns:integration_test_results">
 <html>
 <body>
  <h2 align="center">Test Report</h2>
  <table border="1" align="center">
   <tr bgcolor="orange">
    <Th colspan="2">Results </Th> 
   </tr>
   <tr>
    <th align="Left" bgcolor="orange">Tests passed/Failed/Skipped:</th>
    <td>
     <xsl:value-of select="count(test[status='PASSED'])" /> &#47;
     <xsl:value-of select="count(test[status='FAILED'])" /> &#47;
     <xsl:value-of select="count(test[status='RUNNING'])" /> 
    </td>
   </tr>

   <tr>
    <th align="Left" bgcolor="orange">Started on:</th>
    <xsl:for-each select="test">
    <xsl:sort select="start_time" order="ascending" data-type="text" />
    <xsl:if test="position()=1">
    <TD>
     <xsl:value-of select="start_time" />
    </TD>
    </xsl:if>
    </xsl:for-each>
   </tr>
   <tr>
    <th align="Left" bgcolor="orange">Total time:</th> 
    <td>
     <xsl:value-of select="sum(test/test_duration[number(.)=number(.)])" /> 
    </td>

   </tr>
   <tr>
    <th align="Left" bgcolor="orange">Included groups:</th>
    <td>
     <!-- <xsl:value-of select="" /> -->
    </td>
   </tr>
   <tr>
    <th align="Left" bgcolor="orange">Excluded groups:</th>
    <td>
     <!-- <xsl:value-of select="" /> -->
    </td>
   </tr>
  </table>
  <br></br>
  <table border="1">
   <tr bgcolor="orange">
    <th rowspan="2">Test Name</th>
    <th rowspan="2">Test Results</th>
    <th rowspan="2">Start Time(sec)</th>
    <th rowspan="2">End Time(sec)</th>
    <th rowspan="2">Test Duration(sec)</th>
    <th rowspan="2">Message</th>
    <th colspan="5">DM JOB</th>

   </tr>
   <tr bgcolor="orange">
    <th>Job Name</th>
    <th>Job Results</th>
    <th>Start Time(sec)</th>
    <th>End Time(sec)</th>
    <th>Job Duration(sec)</th>
   </tr>
   <xsl:for-each select="test">
   <xsl:sort select="start_time" order="ascending" data-type="text" />
   <tr>
    <td>
     <xsl:value-of select="name" />
    </td>
    <td>
     <xsl:value-of select="status"/>
    </td>
    <td>
     <xsl:value-of select="start_time" />
    </td> 
    <td>
     <xsl:value-of select="finish_time" />
    </td>
    <td>
     <xsl:value-of select="test_duration"/>
    </td>
    <td>
     <xsl:value-of select="message" />
    </td> 
    <xsl:for-each select="datamover_job">
     <td>
      <xsl:value-of select="name" />
     </td>
     <td>
      <xsl:value-of select="status"/>
     </td>
     <td>
      <xsl:value-of select="start_time" />
     </td> 
     <td>
      <xsl:value-of select="finish_time" />
     </td>     
     <td>
      <xsl:value-of select="job_duration"/>
     </td>
    </xsl:for-each>
   </tr>
   </xsl:for-each>
  </table>
 </body>
 </html>
 </xsl:template>
</xsl:stylesheet>

- 由于

2 个答案:

答案 0 :(得分:0)

首先,正确地说,没有xmlns属性:这是名称空间声明。

其次,您在样式表的XPath表达式中处理命名空间的方式不一致:您已将根元素与正确的命名空间ns:integration_test_results匹配,但之后您不再选择具有该命名空间的后代。

这是FAQ:命名空间声明会传播到后代。

答案 1 :(得分:0)

尝试使用前缀

为其余元素添加前缀
match="ns:integration_test_results"

在xml中,您拥有默认命名空间。在xsl中,您将前缀ns绑定到同一名称空间。因此,您需要为其余元素添加前缀。

这是一个部分变化。对不起,我没有时间休息,但你可以自己解决..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://schemas.oracle.com/dm/v2009">
<xsl:template match="ns:integration_test_results">
    <html>
        <body>
            <h2 align="center">Test Report</h2>
            <table border="1" align="center">
                <tr bgcolor="orange">
                    <Th colspan="2">Results </Th>
                </tr>
                <tr>
                    <th align="Left" bgcolor="orange">Tests passed/Failed/Skipped:</th>
                    <td>
                        <xsl:value-of select="count(ns:test[ns:status='PASSED'])"/> &#47;
 <xsl:value-of select="count(ns:test[ns:status='FAILED'])"/> &#47;
 <xsl:value-of select="count(ns:test[ns:status='RUNNING'])"/>
                    </td>
                </tr>
                <tr>
                    <th align="Left" bgcolor="orange">Started on:</th>
                    <xsl:for-each select="ns:test">
                        <xsl:sort select="ns:start_time" order="ascending" data-type="text"/>
                        <xsl:if test="position()=1">
                            <TD>
                                <xsl:value-of select="ns:start_time"/>
                            </TD>
                        </xsl:if>
                    </xsl:for-each>
                </tr>
                <tr>
                    <th align="Left" bgcolor="orange">Total time:</th>
                    <td>
                        <xsl:value-of select="sum(/ns:test/ns:test_duration[number(.)=number(.)])"/>
                    </td>
                </tr>
                <tr>
                    <th align="Left" bgcolor="orange">Included groups:</th>
                    <td>
                        <!-- <xsl:value-of select="" /> -->
                    </td>
                </tr>
                <tr>
                    <th align="Left" bgcolor="orange">Excluded groups:</th>
                    <td>
                        <!-- <xsl:value-of select="" /> -->
                    </td>
                </tr>
            </table>
            <br/>
            <table border="1">
                <tr bgcolor="orange">
                    <th rowspan="2">Test Name</th>
                    <th rowspan="2">Test Results</th>
                    <th rowspan="2">Start Time(sec)</th>
                    <th rowspan="2">End Time(sec)</th>
                    <th rowspan="2">Test Duration(sec)</th>
                    <th rowspan="2">Message</th>
                    <th colspan="5">DM JOB</th>
                </tr>
                <tr bgcolor="orange">
                    <th>Job Name</th>
                    <th>Job Results</th>
                    <th>Start Time(sec)</th>
                    <th>End Time(sec)</th>
                    <th>Job Duration(sec)</th>
                </tr>
                <xsl:for-each select="ns:test">
                    <xsl:sort select="ns:start_time" order="ascending" data-type="text"/>
                    <tr>
                        <td>
                            <xsl:value-of select="ns:name"/>
                        </td>
                        <td>
                            <xsl:value-of select="ns:status"/>
                        </td>
                        <td>
                            <xsl:value-of select="ns:start_time"/>
                        </td>
                        <td>
                            <xsl:value-of select="ns:finish_time"/>
                        </td>
                        <td>
                            <xsl:value-of select="ns:test_duration"/>
                        </td>
                        <td>
                            <xsl:value-of select="ns:message"/>
                        </td>
                        <xsl:for-each select="ns:datamover_job">
                            <td>
                                <xsl:value-of select="ns:name"/>
                            </td>
                            <td>
                                <xsl:value-of select="ns:status"/>
                            </td>
                            <td>
                                <xsl:value-of select="ns:start_time"/>
                            </td>
                            <td>
                                <xsl:value-of select="ns:finish_time"/>
                            </td>
                            <td>
                                <xsl:value-of select="ns:job_duration"/>
                            </td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>