xslt和xml名称空间和属性

时间:2011-02-24 10:07:36

标签: xml xslt

好的,我有这个xml文件

   <?xml version="1.0" encoding="us-ascii"?>
<?xml-stylesheet type="text/xsl" href="logstyle.xslt"?>
<Events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ConsoleEvent>
        <message>Settings file loaded.</message>
        <date>2011-02-24T02:49:21.9063187-06:00</date>
    </ConsoleEvent>
    <ConsoleEvent type="ConsoleEventError">
        <message>Invalid command 'lol'.</message>
        <date>2011-02-24T02:49:23.9734369-06:00</date>
        <ExceptionMessage>Invalid console command.</ExceptionMessage>
        <StackTrace>No Stack Trace Message Data.</StackTrace>
    </ConsoleEvent>
    <ConsoleEvent xsi:type="ConsoleEventError">
        <message>Invalid command 'yo'.</message>
        <date>2011-02-24T02:49:24.9124907-06:00</date>
        <ExceptionMessage>Invalid console command.</ExceptionMessage>
        <StackTrace>No Stack Trace Message Data.</StackTrace>
    </ConsoleEvent>
    <ConsoleEvent xsi:type="ConsoleEventError">
        <message>Invalid command 'hello'.</message>
        <date>2011-02-24T02:49:25.9915524-06:00</date>
        <ExceptionMessage>Invalid console command.</ExceptionMessage>
        <StackTrace>No Stack Trace Message Data.</StackTrace>
    </ConsoleEvent>
    <ConsoleEvent>
        <message>Baking args brah...
</message>
        <date>2011-02-24T02:49:28.1296747-06:00</date>
    </ConsoleEvent>
    <ConsoleEvent>
        <message>Baking args brah...
b = vag 
a = gav
</message>
        <date>2011-02-24T02:49:38.7152801-06:00</date>
    </ConsoleEvent>
    <ConsoleEvent>
        <message>Quitting...</message>
        <date>2011-02-24T02:49:39.8563454-06:00</date>
    </ConsoleEvent>
</Events>

我试图找到一种选择行的方法。如果他们没有xsi命名空间,我可以做得很好,但我无法弄清楚如何用命名空间选择

以下是xsi

的内容
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="html"  encoding="utf-16"/>
  <xsl:template match="Events">
    <head>
      <title>Event Log</title>
      <style type="text/css">
        body{ text-align: left; width: 100%;  font-family: Verdana, sans-serif; }

        table{ border: none;  border-collapse: separate;  width: 100%; }

        tr.title td{ font-size: 24px;  font-weight: bold; }

        th{ background: #d0d0d0;  font-weight: bold;  font-size: 10pt;  text-align: left; }
        tr{ background: #eeeeee}
        td, th{ font-size: 8pt;  padding: 1px;  border: none; }

        tr.info td{}
        tr.warning td{background-color:yellow;color:black}
        tr.error td{background-color:red;color:black}

        span {text-decoration:underline}
        a:hover{text-transform:uppercase;color: #9090F0;}
      </style>
    </head>

    <body>
      <table>
        <tr class="title">
          <td colspan="7">Event Log</td>
        </tr>
        <tr>
          <td colspan="2">Standard Events</td>
          <td colspan="5">
            <xsl:value-of select="count(//ConsoleEvent)"/>
          </td>
        </tr>
        <tr>
          <td colspan="2">Errors</td>
          <td colspan="5">
            <xsl:value-of select="count(//ConsoleEvent[@type='ConsoleEventError'])"/>
          </td>
        </tr>
        <xsl:apply-templates/>
      </table>

    </body>
  </xsl:template>

</xsl:stylesheet>

2 个答案:

答案 0 :(得分:2)

将命名空间添加到样式表。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   ...
            <xsl:value-of select="count(//ConsoleEvent[@xsi:type='ConsoleEventError'])"/>
   ...

答案 1 :(得分:1)

解决方案(这次记住规则!):

  1. xsi namespace定义添加到您的XSLT样式表中:

  2. 相应地更改代码

            <tr>
                <td colspan="2">Errors</td>
                <td colspan="5">
                    <xsl:value-of select=
                    "count(//ConsoleEvent
                              [@type='ConsoleEventError'
                             or
                               @xsi:type='ConsoleEventError'
                              ]
                          )"/>
                </td>
            </tr>
    
  3. 您的整个代码

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    exclude-result-prefixes="xsi">
        <xsl:output method="html"  encoding="utf-16"/>
        <xsl:template match="Events">
            <head>
                <title>Event Log</title>
                <style type="text/css">
                   body{ text-align: left; width: 100%;  font-family: Verdana, sans-serif; }          table{ border: none;  border-collapse: separate;  width: 100%; }          tr.title td{ font-size: 24px;  font-weight: bold; }          th{ background: #d0d0d0;  font-weight: bold;  font-size: 10pt;  text-align: left; }         tr{ background: #eeeeee}         td, th{ font-size: 8pt;  padding: 1px;  border: none; }          tr.info td{}         tr.warning td{background-color:yellow;color:black}         tr.error td{background-color:red;color:black}          span {text-decoration:underline}         a:hover{text-transform:uppercase;color: #9090F0;}       </style>
            </head>
            <body>
                <table>
                    <tr class="title">
                        <td colspan="7">Event Log</td>
                    </tr>
                    <tr>
                        <td colspan="2">Standard Events</td>
                        <td colspan="5">
                            <xsl:value-of select="count(//ConsoleEvent)"/>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">Errors</td>
                        <td colspan="5">
                            <xsl:value-of select=
                            "count(//ConsoleEvent
                                      [@type='ConsoleEventError'
                                     or
                                       @xsi:type='ConsoleEventError'
                                      ]
                       )"/>
                        </td>
                    </tr>
                    <xsl:apply-templates/>
                </table>
            </body>
        </xsl:template>
    </xsl:stylesheet>
    

    并获得正确的结果

    <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-16">
    
       <title>Event Log</title><style type="text/css">
                   body{ text-align: left; width: 100%;  font-family: Verdana, sans-serif; }          table{ border: none;  border-collapse: separate;  width: 100%; }          tr.title td{ font-size: 24px;  font-weight: bold; }          th{ background: #d0d0d0;  font-weight: bold;  font-size: 10pt;  text-align: left; }         tr{ background: #eeeeee}         td, th{ font-size: 8pt;  padding: 1px;  border: none; }          tr.info td{}         tr.warning td{background-color:yellow;color:black}         tr.error td{background-color:red;color:black}          span {text-decoration:underline}         a:hover{text-transform:uppercase;color: #9090F0;}       </style></head>
    <body>
       <table>
          <tr class="title">
             <td colspan="7">Event Log</td>
          </tr>
          <tr>
             <td colspan="2">Standard Events</td>
             <td colspan="5">7</td>
          </tr>
          <tr>
             <td colspan="2">Errors</td>
             <td colspan="5">3</td>
          </tr>
    
                Settings file loaded.
                2011-02-24T02:49:21.9063187-06:00
    
    
                Invalid command 'lol'.
                2011-02-24T02:49:23.9734369-06:00
                Invalid console command.
                No Stack Trace Message Data.
    
    
                Invalid command 'yo'.
                2011-02-24T02:49:24.9124907-06:00
                Invalid console command.
                No Stack Trace Message Data.
    
    
                Invalid command 'hello'.
                2011-02-24T02:49:25.9915524-06:00
                Invalid console command.
                No Stack Trace Message Data.
    
    
                Baking args brah... 
                2011-02-24T02:49:28.1296747-06:00
    
    
                Baking args brah... b = vag  a = gav 
                2011-02-24T02:49:38.7152801-06:00
    
    
                Quitting...
                2011-02-24T02:49:39.8563454-06:00
    
    
       </table>
    </body>