好的,我不确定如何描述我的问题,基本上我有一个XML文件我试图通过在XML文件中搜索包含字符串的节点来获取某些信息
我的问题是,如果我只包含我感兴趣的<table>
标记而不是整个XML文件,则以下脚本可以完成这项工作。这就是我称之为输入二,它工作正常。
但如果我使用整个XML文件它不起作用,这是脚本:
(tidy -asxml input.xml | xmllint --xpath 'descendant-or-self::*[starts-with(text(), "Aktiv tid:")]/following-sibling::*/text()' -) 2>/dev/null
这是输入的一个XML文件(完整的XML文件):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SpeedTouch - Bredbandsanslutning</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">var g_navitem = -1;</script>
<script type="text/javascript"> var g_focus = -1;</script>
<script type='text/javascript' src='/util.js'></script>
<link rel="stylesheet" type="text/css" href="/styles.css">
</head>
<body onLoad="setFocus();" height="100%" style="margin:0px">
<noscript>
<h1>Thomson - SpeedTouch</h1>
<h4>To view the Web interface of your device, JavaScript must be supported and enabled on your browser! <br><br>Aktivera skriptstöd och uppdatera webbläsaren.</h4>
</noscript>
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="background-color:white" height="100%">
<tr>
<td colspan="2">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td style="padding-left:15px;" class="Product">THOMSON ST780</td><td align="right" style="padding:5px 15px 0px 0px;"><a href="http://www.thomson-broadband.com"><img src="/images/Thomson.gif" border="0" width="109" height="50" alt="THOMSON logo"></a></td>
</tr>
<tr>
<td colspan="2">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr style="background-image:url(/images/bar.gif)">
<td width="20%"></td>
<td width="10" align="left"></td>
<td width="10"><img src="/images/barend_left.gif"></td>
<td><img width="100%" height="10" src="/images/spacer_white.gif"></td>
</tr>
<tr style="background-image:url(/images/bar.gif)">
<td align="right"><img width="100%" height="10" src="/images/spacer_white.gif"></td>
<td width="10"><img src="/images/barend_right.gif"></td>
<td colspan="2"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td><td align="right" valign="middle" style="padding-right:15px"><form name="langSelect" action="/cgi/language.cgi" method=post><span class="langSelect"><input type="hidden" name=6 value="en">
<a href="" onClick="setLanguage('en');submitForm(document.langSelect,0);return false;" title="English">en</a>
<strong>sv</strong> </span></form></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2"><img src="/images/spacer.gif" border="0" width="1" height="10" alt=""><br></td>
</tr>
<tr>
<td valign="top" style="padding-top:15px;padding-left:15px;">
<script type="text/javascript">writeMenu();</script>
</td>
<td valign="top" style="background:url(/images/wave.gif) no-repeat top center;height:340px">
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:15px">
<script type="text/javascript">writeNavBar();</script>
<tr>
<td>
<table width="700" cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<script type="text/javascript">pm_write_messages();</script>
<div class='contentcontainer'>
<hr>
<div class='contentitem'>
<table cellspacing='0' cellpadding='0'>
<tr><td class='icon' valign='top' width='100px'><img src='/images/cplngrxl.gif' alt='Fysisk anslutning OK'></td>
<td class='data' valign='top'><table cellspacing='0' cellpadding='0'><tr><td align='left'><span class='itemtitle'>DSL-anslutning</span></td><td align='right'></td></tr>
<tr><td colspan='2'><br><table cellspacing='0' cellpadding='0' width='100%'><tr><td width='40' valign='top'><img src='/images/bull__md.gif' alt=''></td><td valign='top'>
<span class='blocktitle'><a href="javascript:GoAndRemember('/cgi/b/dsl/ov/', '')">Visa mer...</a></span><br>
<table width='100%' class='datatable' cellspacing='0' cellpadding='0'>
<tr><td></td><td width='30px'></td><td width='220px'></td><td width='50px'></td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Aktiv tid:</td><td colspan='3'>1 dag, 21:44:06</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Bandbredd (upp/ned) [kbps/kbps]:</td><td colspan='3'>1.058 / 21.373</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Överförda data (skickade/mottagna) [GB/GB]:</td><td colspan='3'>1,97 / 45,23</td></tr>
</table>
</td></tr></table>
</td></tr></table></td></tr></table></div>
<hr>
<div class='contentitem'>
<table cellspacing='0' cellpadding='0'>
<tr><td class='icon' valign='top' width='100px'><img src='/images/cintgrxl.gif' alt='Internetanslutning OK'></td>
<td class='data' valign='top'><table cellspacing='0' cellpadding='0'><tr><td align='left'><span class='itemtitle'>Internet</span></td><td align='right'></td></tr>
<tr><td colspan='2'><br><table cellspacing='0' cellpadding='0' width='100%'><tr><td width='40' valign='top'><img src='/images/bull__md.gif' alt=''></td><td valign='top'>
<span class='blocktitle'><a href="javascript:GoAndRemember('/cgi/b/is/_ethoa_/ov/', 'name=Internet')">Visa mer...</a></span><br>
<table width='100%' class='datatable' cellspacing='0' cellpadding='0'>
<tr><td></td><td width='30px'></td><td width='220px'></td><td width='50px'></td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Typ:</td><td colspan='3'>ETHoA</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Aktiv tid:</td><td colspan='3'>1 dag, 21:44:04</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>IP-adress:</td><td colspan='3'>x.x.x.x</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Överförda data (skickade/mottagna) [GB/GB]:</td><td colspan='3'>1,56 / 39,92</td></tr>
</table>
</td></tr></table>
</td></tr></table></td></tr></table></div>
</form>
<script type='text/javascript'>generateTasks()</script>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
这是输入的两个XML文件:
<hr>
<div class='contentitem'>
<table cellspacing='0' cellpadding='0'>
<tr><td class='icon' valign='top' width='100px'><img src='/images/cplngrxl.gif' alt='Fysisk anslutning OK'></td>
<td class='data' valign='top'><table cellspacing='0' cellpadding='0'><tr><td align='left'><span class='itemtitle'>DSL-anslutning</span></td><td align='right'></td></tr>
<tr><td colspan='2'><br><table cellspacing='0' cellpadding='0' width='100%'><tr><td width='40' valign='top'><img src='/images/bull__md.gif' alt=''></td><td valign='top'>
<span class='blocktitle'><a href="javascript:GoAndRemember('/cgi/b/dsl/ov/', '')">Visa mer...</a></span><br>
<table width='100%' class='datatable' cellspacing='0' cellpadding='0'>
<tr><td></td><td width='30px'></td><td width='220px'></td><td width='50px'></td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Aktiv tid:</td><td colspan='3'>1 dag, 21:44:06</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Bandbredd (upp/ned) [kbps/kbps]:</td><td colspan='3'>1.058 / 21.373</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Överförda data (skickade/mottagna) [GB/GB]:</td><td colspan='3'>1,97 / 45,23</td></tr>
</table>
</td></tr></table>
</td></tr></table></td></tr></table></div>
<hr>
<div class='contentitem'>
<table cellspacing='0' cellpadding='0'>
<tr><td class='icon' valign='top' width='100px'><img src='/images/cintgrxl.gif' alt='Internetanslutning OK'></td>
<td class='data' valign='top'><table cellspacing='0' cellpadding='0'><tr><td align='left'><span class='itemtitle'>Internet</span></td><td align='right'></td></tr>
<tr><td colspan='2'><br><table cellspacing='0' cellpadding='0' width='100%'><tr><td width='40' valign='top'><img src='/images/bull__md.gif' alt=''></td><td valign='top'>
<span class='blocktitle'><a href="javascript:GoAndRemember('/cgi/b/is/_ethoa_/ov/', 'name=Internet')">Visa mer...</a></span><br>
<table width='100%' class='datatable' cellspacing='0' cellpadding='0'>
<tr><td></td><td width='30px'></td><td width='220px'></td><td width='50px'></td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Typ:</td><td colspan='3'>ETHoA</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Aktiv tid:</td><td colspan='3'>1 dag, 21:44:04</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>IP-adress:</td><td colspan='3'>x.x.x.x</td></tr>
<tr><td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td></tr>
<tr><td width='170'>Överförda data (skickade/mottagna) [GB/GB]:</td><td colspan='3'>1,56 / 39,92</td></tr>
</table>
</td></tr></table>
</td></tr></table></td></tr></table></div>
</form>
<script type='text/javascript'>generateTasks()</script>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
因此,出于某种原因,如果我删除了此行之前的所有内容:
<div class='contentcontainer'>
脚本运行正常。
这对我来说似乎很奇怪,但似乎是一个非常基本的问题。
所以我的问题是如何解决这个问题?
提前致谢!
答案 0 :(得分:3)
如果您尝试仅运行管道的第一部分来检查其输出,您会注意到
tidy -asxml input.xml
不返回给定文件的数据。
这是由于虚假的</form>
(因为该文档只包含一个<form>
但只包含两个</form>
) - HTMLTidy 在其写入的消息中明确告诉您到stderr ,如果你选择阅读它。
简而言之:虽然Tidy可以使用警告清理文档,但任何包含错误的文档都需要在处理之前进行修复。