我怎样才能用xslt从xml中选择必要的东西?

时间:2015-05-26 17:48:52

标签: xml parsing xslt

我使用xslt时很新。

我得到了以下xml-output:

    <valgrindoutput>

    <protocolversion>4</protocolversion>
    <protocoltool>memcheck</protocoltool>

    <preamble>
      <line>Memcheck, a memory error detector</line>
      <line>Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.</line>
      <line>Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info</line>
      <line>Command: ./a.out</line>
    </preamble>

    <pid>5746</pid>
    <ppid>5427</ppid>
    <tool>memcheck</tool>

    <args>
      <vargv>
        <exe>/usr/bin/valgrind.bin</exe>
        <arg>--xml=yes</arg>
        <arg>--xml-file=b.xml</arg>
        <arg>--tool=memcheck</arg>
      </vargv>
      <argv>
        <exe>./a.out</exe>
      </argv>
    </args>

    <status>
      <state>RUNNING</state>
      <time>00:00:00:00.198 </time>
    </status>

    <error>
      <unique>0x0</unique>
      <tid>1</tid>
      <kind>MismatchedFree</kind>
      <what>Mismatched free() / delete / delete []</what>
      <stack>
        <frame>
          <ip>0x402B838</ip>
          <obj>/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so</obj>
          <fn>operator delete(void*)</fn>
        </frame>
        <frame>
          <ip>0x8048671</ip>
          <obj>/home/pg/pse/vonDominik/valgrind/a.out</obj>
          <fn>main</fn>
          <dir>/home/pg/pse/vonDominik/valgrind/.</dir>
          <file>memory_check_fehler.cpp</file>
          <line>7</line>
        </frame>
      </stack>
      <auxwhat>Address 0x4348028 is 0 bytes inside a block of size 1 alloc'd</auxwhat>
      <stack>
        <frame>
          <ip>0x402A17C</ip>
          <obj>/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so</obj>
          <fn>malloc</fn>
        </frame>
        <frame>
          <ip>0x8048651</ip>
          <obj>/home/pg/pse/vonDominik/valgrind/a.out</obj>
          <fn>main</fn>
          <dir>/home/pg/pse/vonDominik/valgrind/.</dir>
          <file>memory_check_fehler.cpp</file>
          <line>5</line>
        </frame>
      </stack>
    </error>

    <error>
      <unique>0x1</unique>
      <tid>1</tid>
      <kind>MismatchedFree</kind>
      <what>Mismatched free() / delete / delete []</what>
      <stack>
        <frame>
          <ip>0x402B3D8</ip>
          <obj>/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so</obj>
          <fn>free</fn>
        </frame>
        <frame>
          <ip>0x804867D</ip>
          <obj>/home/pg/pse/vonDominik/valgrind/a.out</obj>
          <fn>main</fn>
          <dir>/home/pg/pse/vonDominik/valgrind/.</dir>
          <file>memory_check_fehler.cpp</file>
          <line>8</line>
        </frame>
      </stack>
      <auxwhat>Address 0x4348060 is 0 bytes inside a block of size 1 alloc'd</auxwhat>
      <stack>
        <frame>
          <ip>0x402A6DC</ip>
          <obj>/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so</obj>
          <fn>operator new(unsigned int)</fn>
        </frame>
        <frame>
          <ip>0x8048661</ip>
          <obj>/home/pg/pse/vonDominik/valgrind/a.out</obj>
          <fn>main</fn>
          <dir>/home/pg/pse/vonDominik/valgrind/.</dir>
          <file>memory_check_fehler.cpp</file>
          <line>6</line>
        </frame>
      </stack>
    </error>


    <status>
      <state>FINISHED</state>
      <time>00:00:00:02.262 </time>
    </status>

    <errorcounts>
      <pair>
        <count>1</count>
        <unique>0x1</unique>
      </pair>
      <pair>
        <count>1</count>
        <unique>0x0</unique>
      </pair>
    </errorcounts>

    <suppcounts>
    </suppcounts>

    </valgrindoutput>

我需要的内容是: 我需要标签之间的部分。

  <kind>MismatchedFree</kind>
  <what>Mismatched free() / delete / delete []</what>
  <file>memory_check_fehler.cpp</file>
  <line>5</line>

但我无法得到输出。 我按照xslt命令尝试:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/valgrindoutput">
<caption>Error summary</caption>
  <html>
  <body>
  <table>
  <thead>
  <tr>
  <th>Filename</th>
  <th>Line</th>
  <th>Testname</th>
  <th>Severity</th>
  <th>Severity_description</th>
  </tr>
  </thead>

  <tbody>
    <xsl:apply-templates select="error"/>
  </tbody>

  </table>
  </body>
  </html>
 </xsl:template>

<xsl:template match="error">
 <tr>
  <td><xsl:value-of select="@file"/></td>
  <td><xsl:value-of select="@line"/></td>
  <td><xsl:value-of select="@valgrind"/></td>
  <td><xsl:value-of select="@kind"/></td>
  <td><xsl:value-of select="@what"/></td>
 </tr>
</xsl:template>

</xsl:stylesheet>

但是输出是空的!

模板中的模板也不起作用:

 <xsl:template match="error">
     <tr>
       <xsl:template match="file">
       <td><xsl:value-of select="."/></td>
       </xsl:template>
       <td><xsl:value-of select="@line"/></td>
       <td><xsl:value-of select="@valgrind"/></td>
       <td><xsl:value-of select="@kind"/></td>
       <td><xsl:value-of select="@what"/></td>
    </tr>
 </xsl:template>
希望你能帮助我;)

1 个答案:

答案 0 :(得分:1)

AFAICT,你想做类似的事情:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/valgrindoutput">
    <table border="1">
        <caption>Error summary</caption>
        <thead>
            <tr>
                <th>Kind</th>
                <th>What</th>
                <th>File</th>
                <th>Line</th>
            </tr>
        </thead>
        <tbody>
            <xsl:apply-templates select="error/stack"/>
        </tbody>
    </table>
 </xsl:template>

<xsl:template match="stack">
    <tr>
        <td><xsl:value-of select="../kind"/></td>
        <td><xsl:value-of select="../what"/></td>
        <td><xsl:value-of select="frame/file"/></td>
        <td><xsl:value-of select="frame/line"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

生产:

enter image description here