【问题标题】:How can i pick only necessary out of xml with xslt?如何使用 xslt 从 xml 中只选择必要的?
【发布时间】:2015-08-08 12:50:49
【问题描述】:

我对使用 xslt 还是很陌生。

我得到了以下 xml 输出:

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

希望你能帮助我;)

【问题讨论】:

  • 请修正您的输入,使其成为格式良好的 XML 文档(没有相应的结束标签的开始标签)。

标签: xml parsing xslt


【解决方案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>

生产:

【讨论】:

  • 如何获得输出?我得到一个空输出! xsltproc blanco.xsl b.xml >abc.html 不起作用...
  • 现在我看到了问题所在....在创建此标签时,我损坏了 xml 文件..我如何到达类似行的标签? ../line 或 frame/line 我不明白.. 我现在上传原始文件....
  • 是的,它现在工作正常!我怎样才能提取这个结构: value value_n..... ?因为如果我将
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2019-09-20
相关资源
最近更新 更多