xmlstarlet - 从 XML 文件中提取属性值 (starlet)
问题描述
我有一个包含五个条目的小 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<posts>
<row Id="4" PostTypeId="1" AcceptedAnswerId="7" CreationDate="2008-07-31T21:42:52.667" Score="630" ViewCount="42817" Body="<p>I want to use a track-bar to change a form's opacity.</p>

<p>This is my code:</p>

<pre><code>decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;
</code></pre>

<p>When I build the application, it gives the following error:</p>

<blockquote>
 <p>Cannot implicitly convert type <code>'decimal'</code> to <code>'double'</code></p>
</blockquote>

<p>I tried using <code>trans</code> and <code>double</code> but then the control doesn't work. This code worked fine in a past VB.NET project.</p>
" OwnerUserId="8" LastEditorUserId="3641067" LastEditorDisplayName="Rich B" LastEditDate="2019-07-19T01:39:54.173" LastActivityDate="2019-07-19T01:39:54.173" Title="Convert Decimal to Double?" Tags="<c#><floating-point><type-conversion><double><decimal>" AnswerCount="13" CommentCount="2" FavoriteCount="48" CommunityOwnedDate="2012-10-31T16:42:47.213" />
<row Id="6" PostTypeId="1" AcceptedAnswerId="31" CreationDate="2008-07-31T22:08:08.620" Score="281" ViewCount="18214" Body="<p>I have an absolutely positioned <code>div</code> containing several children, one of which is a relatively positioned <code>div</code>. When I use a <code>percentage-based width</code> on the child <code>div</code>, it collapses to <code>0 width</code> on IE7, but not on Firefox or Safari.</p>

<p>If I use <code>pixel width</code>, it works. If the parent is relatively positioned, the percentage width on the child works.</p>

<ol>
<li>Is there something I'm missing here?</li>
<li>Is there an easy fix for this besides the <code>pixel-based width</code> on the
child?</li>
<li>Is there an area of the CSS specification that covers this?</li>
</ol>
" OwnerUserId="9" LastEditorUserId="3641067" LastEditorDisplayName="Rich B" LastEditDate="2019-07-19T01:43:04.077" LastActivityDate="2019-07-19T01:43:04.077" Title="Percentage width child element in absolutely positioned parent on Internet Explorer 7" Tags="<html><css><internet-explorer-7>" AnswerCount="6" CommentCount="0" FavoriteCount="10" />
<row Id="7" PostTypeId="2" ParentId="4" CreationDate="2008-07-31T22:17:57.883" Score="425" Body="<p>An explicit cast to double like this isn't necessary:</p>

<pre><code>double trans = (double) trackBar1.Value / 5000.0;
</code></pre>

<p>Identifying the constant as <code>5000.0</code> (or as <code>5000d</code>) is sufficient:</p>

<pre><code>double trans = trackBar1.Value / 5000.0;
double trans = trackBar1.Value / 5000d;
</code></pre>
" OwnerUserId="9" LastEditorUserId="4020527" LastEditDate="2017-12-16T05:06:57.613" LastActivityDate="2017-12-16T05:06:57.613" CommentCount="0" />
<row Id="9" PostTypeId="1" AcceptedAnswerId="1404" CreationDate="2008-07-31T23:40:59.743" Score="1742" ViewCount="555183" Body="<p>Given a <code>DateTime</code> representing a person's birthday, how do I calculate their age in years? </p>
" OwnerUserId="1" LastEditorUserId="3956566" LastEditorDisplayName="Rich B" LastEditDate="2018-04-21T17:48:14.477" LastActivityDate="2019-06-26T15:25:44.253" Title="How do I calculate someone's age in C#?" Tags="<c#><.net><datetime>" AnswerCount="63" CommentCount="5" FavoriteCount="436" CommunityOwnedDate="2011-08-16T19:40:43.080" />
<row Id="11" PostTypeId="1" AcceptedAnswerId="1248" CreationDate="2008-07-31T23:55:37.967" Score="1444" ViewCount="149445" Body="<p>Given a specific <code>DateTime</code> value, how do I display relative time, like:</p>

<ul>
<li>2 hours ago</li>
<li>3 days ago</li>
<li>a month ago</li>
</ul>
" OwnerUserId="1" LastEditorUserId="6479704" LastEditorDisplayName="user2370523" LastEditDate="2017-06-04T15:51:19.780" LastActivityDate="2019-05-26T02:31:53.863" Title="Calculate relative time in C#" Tags="<c#><datetime><time><datediff><relative-time-span>" AnswerCount="37" CommentCount="3" FavoriteCount="539" CommunityOwnedDate="2009-09-04T13:15:59.820" />
</posts>
我有兴趣从 5 个条目中仅提取标题和正文。到目前为止,我能想到的最好的方法是使用 starlet:
xmlstarlet sel -T -t -m '/posts/row' -v "concat(@Body,'|', @Title)" -n very_small_posts.xml
very_small_posts.xml 是包含上述 5 个条目的 xml 文件。问题是输出中有 XML 标签,我也看到空行。如下所示(第一行的输出):
<p>I want to use a track-bar to change a form's opacity.</p>
<p>This is my code:</p>
<pre><code>decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;
</code></pre>
<p>When I build the application, it gives the following error:</p>
<blockquote>
<p>Cannot implicitly convert type <code>'decimal'</code> to <code>'double'</code></p>
</blockquote>
<p>I tried using <code>trans</code> and <code>double</code> but then the control doesn't work. This code worked fine in a past VB.NET project.</p>
|Convert Decimal to Double?
有没有办法清理它们?我只想看到没有任何标签的常规文本作为输出......并且可能在值之前带有属性名称。我不想<p>, </p>, <blockquote>
在我的输出中看到 , .. 有什么建议么?
首选输出(第一行):
Body=I want to use a track-bar to change a form's opacity. This is my code: decimal trans = trackBar1.Value / 5000; this.Opacity = trans; When I build the application, it gives the following error: Cannot implicitly convert type 'decimal' to 'double'I tried using trans and double but then the control doesn't work. This code worked fine in a past VB.NET project.
Title=Convert Decimal to Double?
解决方案
如果原始帖子中的输出样本(包含标签)在 XML 文件中,则很容易提取规范化的文本,因此要生成所需的输出并查询 XML 包装器:
xmlstarlet sel -t \
-e doc \
-m 'posts/row' \
-e Body -v '@Body' -b \
-e Title -v '@Title' -b \
file.xml |
xmlstarlet unesc |
xmlstarlet sel --text -t -m '*/*' -v 'concat(name(),"=",normalize-space())' --nl
在哪里:
- 1st
sel
创建容器元素@Body
和@Title
内容 unesc
<
转换 XML 字符实体,例如<
- 2nd
sel
将二级元素提取为以元素名称为前缀的规范化文本
当然不建议使用unesc
这种方式来修改文档结构,但在控制时它是可行的。
链接到xmlstarlet
用户指南。
推荐阅读
- javascript - 如何从反应js中的父组件状态更新子组件下拉列表中的选项值?
- powerbi - 活跃和不活跃的客户
- django-rest-framework - 数据库中是否存在记录?Django Api 通用视图
- apache - 为什么需要在配置单元中为动态分区设置属性
- regex - Scala 函数式编程概念而不是多个 for 循环
- cypress - 如何使用赛普拉斯启用浏览器通知
- r - 按组 R 在列中分配值
- c# - 将资源 file.resx 用于 WPF 控件内容属性
- c# - 在 Xamarin 表单中单击多边形时如何显示弹出窗口
- python - 修改python代码以返回列表中项目的乘积而不是总和