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

时间:2014-07-18 09:55:53

标签: xml xpath

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

这是一些xml:

<?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是这样的:

0-3     fred.darwin       25/11/2012      promoting changes

由于我正在比较的数字从'0 - '开始,这很复杂。

幸运的是,你可以删除'0-'并得到一个实数,这对应于一个位置,所以我有这样的东西:

xmllint --xpath '/releaseNote/change[position()>2]/description/text() ${file}

它只是连接所有描述并将它们吐出来。

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

2 个答案:

答案 0 :(得分:1)

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

stylesheet.xsl

<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运行它,例如像这样:

xsltproc --stringparam version 0-2 stylesheet.xsl releaseNote.xml

输出结果为:

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}},所以这也不应该是问题。

答案 1 :(得分:0)

好的,所以我得到了这样的工作:

#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

输出如下:

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时慢得多,但它确实允许我正确地解析日期,它看起来完全符合我的想法。

成功!