2011-02-24 149 views
0

好了,所以我有这个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> 
+0

昨天重复的:卸下XML标记,被命名为xfdf:field(带有命名空间)](http:// stacko verle.com/questions/5098034/removing-an-xml-tag-that-is-named-like-xfdffield-with-a-namespace) – 2011-02-24 12:40:03

+0

@Alejandro Eh?重复?主要的问题是我不知道我必须在我的样式表中使用namsespace。 – 2011-02-24 20:50:00

+0

同样的问题在这个问题。如果使用前缀QName,则需要将该前缀声明/绑定到名称空间URI。 – 2011-02-24 20:55:28

回答

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. xsi namespace定义添加到您的XSLT样式表:

  2. 更改代码 ac科丁:

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

你的整个代码变得

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