我有一些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}把所有的描述连在一起然后吐出来。
如何循环遍历它们并选择多个节点内容?
发布于 2014-07-18 10:48:06
好的,我让它像这样工作:
#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时要慢得多,但它确实允许我正确地解析日期,而且它看起来与我想要的完全一样。
成功!
发布于 2014-07-18 11:52:36
看起来您已经找到了一个解决方案,但只是为了提供一个替代方案:如果您不介意使用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="'	'"/>
<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, '-')) >= $version-after-hyphen]"/>
</xsl:template>
<xsl:template match="change">
<xsl:apply-templates/>
<!-- Insert a newline after every <change> element -->
<xsl:text> </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,所以这也不会是一个问题。
https://stackoverflow.com/questions/24822184
复制相似问题