开发者

xslt and xml namespaces and attributes

Ok so I have this xml file

   <?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>开发者_JAVA百科
        <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>

and I'm trying to find a way to select rows. I can do it fine if they don't have the xsi namespace on them, but I can't figure out how to select with a namespace

Heres what I got for the 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>


Add the namespace to your stylesheet.

<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'])"/>
   ...


Solution (remember the rule this time!):

  1. Add the xsi namespace definition to your XSLT stylesheet:

  2. Change the code accordingly:

            <tr>
                <td colspan="2">Errors</td>
                <td colspan="5">
                    <xsl:value-of select=
                    "count(//ConsoleEvent
                              [@type='ConsoleEventError'
                             or
                               @xsi:type='ConsoleEventError'
                              ]
                          )"/>
                </td>
            </tr>
    

Your whole code becomes:

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

and you get the correct result:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜