首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xpath -如何根据值选择多个节点

xpath -如何根据值选择多个节点
EN

Stack Overflow用户
提问于 2014-07-18 09:55:53
回答 2查看 1.6K关注 0票数 1

我有一些xml,我想根据一些传入的数据动态地提取一些信息。

以下是一些xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<releaseNote>
 <name>DECOUPLING_client</name>
  <change>
   <date hour="12" day="24" second="44" year="2012" month="10" minute="46"/>
   <submitter>Automatically Generated</submitter>
   <description>ReleaseNote Created</description>
  </change>
  <change>
   <version>0-2</version>
   <date hour="12" day="24" second="48" year="2012" month="11" minute="46"/>
   <submitter>fred.darwin</submitter>
   <description> first iteration of decoupling client - copied files from old decoupling module</description>
   <install/>
  </change>
 <change>
  <version>0-3</version>
  <date hour="16" day="25" second="34" year="2012" month="11" minute="52"/>
  <submitter>fred.darwin</submitter>
  <description> promoting changes</description>
  <install/>
 </change>
</releaseNote>

我想传入字符串' 0-2‘,找出从0-2开始的所有版本,如下所示:

代码语言:javascript
复制
0-3     fred.darwin       25/11/2012      promoting changes

更复杂的是,我比较的数字从'0-‘开始。

但是幸运的是,您可以删除'0-‘并得到一个实数,它对应于一个位置,所以我已经了解到了这样的情况:

代码语言:javascript
复制
xmllint --xpath '/releaseNote/change[position()>2]/description/text() ${file}

把所有的描述连在一起然后吐出来。

如何循环遍历它们并选择多个节点内容?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-18 10:48:06

好的,我让它像这样工作:

代码语言:javascript
复制
#first count number of nodes ie how many changes (could be eg 979)
numChanges=`xmllint --xpath 'count(/releaseNote/change)' ${MPATH}/${mod}/resources/ReleaseNote.xml`
echo "found $numChanges changes"
#even though last one shows as 0-952
lastModule=`xmllint --xpath '/releaseNote/change[last()]/version/text()' ${MPATH}/${mod}/resources/ReleaseNote.xml`
#so

#then loop through from our one to the end
#use a unix loop
echo "version number ${versionNumber} num changes ${numChanges}"
#for i in {${versionNumber}..${numChanges}}; do
i=$versionNumber
while [[ $i -le $numChanges ]]  ; do
#and for each item in the list spit out description, version, submitter and try to parse date
xmllint --xpath "/releaseNote/change[$i]/version/text()" ${MPATH}/${mod}/resources/ReleaseNote.xml
echo -en ' \t '
xmllint --xpath "string(/releaseNote/change[$i]/date/@day)" ${MPATH}/${mod}/resources/ReleaseNote.xml
echo -en '/'
xmllint --xpath "string(/releaseNote/change[$i]/date/@month)" ${MPATH}/${mod}/resources/ReleaseNote.xml
echo -en '/'
xmllint --xpath "string(/releaseNote/change[$i]/date/@year)" ${MPATH}/${mod}/resources/ReleaseNote.xml
echo -en ' '
xmllint --xpath "string(/releaseNote/change[$i]/date/@hour)" ${MPATH}/${mod}/resources/ReleaseNote.xml
echo -en ':'
xmllint --xpath "string(/releaseNote/change[$i]/date/@minute)" ${MPATH}/${mod}/resources/ReleaseNote.xml
echo -en ' \t '
xmllint --xpath "/releaseNote/change[$i]/submitter/text()" ${MPATH}/${mod}/resources/ReleaseNote.xml
echo -en ' \t '
xmllint --xpath "/releaseNote/change[$i]/description/text()" ${MPATH}/${mod}/resources/ReleaseNote.xml
((i = i + 1))
echo
done

输出如下:

代码语言:javascript
复制
0-96     21/4/2014 17:56     onkar.sharma     test case for subscription validation
0-97     28/5/2014 15:58     trushant.patel       JIRAET81
0-98     9/6/2014 18:10      vivek.mittal     Refinement for GetFindPackagesWithService flow

它比我使用grep和tr时要慢得多,但它确实允许我正确地解析日期,而且它看起来与我想要的完全一样。

成功!

票数 0
EN

Stack Overflow用户

发布于 2014-07-18 11:52:36

看起来您已经找到了一个解决方案,但只是为了提供一个替代方案:如果您不介意使用XSLT,那么您也可以拥有这样一个样式表文件:

stylesheet.xsl

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="utf-8"/>

  <xsl:param name="version"/>

  <!-- Strip white-space-only text nodes in all elements -->
  <xsl:strip-space elements="*"/>

  <xsl:variable name="version-after-hyphen" select="number(substring-after($version, '-'))"/>

  <!-- XML entity for a tab -->
  <xsl:variable name="DELIMITER" select="'&#9;'"/>

  <xsl:template match="/">
    <!--
    Apply every <change> element with a <version> child whose value is $version
    or greater.
    -->
    <xsl:apply-templates
      select="releaseNote/change[number(substring-after(version, '-')) &gt;= $version-after-hyphen]"/>
  </xsl:template>

  <xsl:template match="change">
    <xsl:apply-templates/>
    <!-- Insert a newline after every <change> element -->
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="submitter | description">
    <xsl:value-of select="concat($DELIMITER, normalize-space(.))"/>
  </xsl:template>

  <xsl:template match="version">
    <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="date">
    <!-- Format date -->
    <xsl:value-of select="concat($DELIMITER, @day, '/', @month, '/', @year, ' ', @hour, ':', @minute)"/>
  </xsl:template>
</xsl:stylesheet>

然后您可以使用xsltproc运行它,例如:

代码语言:javascript
复制
xsltproc --stringparam version 0-2 stylesheet.xsl releaseNote.xml

产出如下:

代码语言:javascript
复制
0-2 24/11/2012 12:46  fred.darwin first iteration of decoupling client - copied files from old decoupling module
0-3 25/11/2012 16:52  fred.darwin promoting changes

我相当肯定,它将比多次执行xmllint更快,从长远来看也可能更容易维护。libxml2 (您已经安装了xmllint)也包括xsltproc,所以这也不会是一个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24822184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档