Ant -如何扫描文件并获得文本的最后特定出现



我有一个大的。xml文件,格式为:

<items>
<item from="" to="" version="">
<subAttribute1>A</subAttribute1>
<subAttribute2>B</subAttribute2>
</item>
<item from="" to="" version="">
<subAttribute1>C</subAttribute1>
<subAttribute2>D</subAttribute2>
</item>
</items>

有没有办法让我可以:

  • 查找/加载文件
  • 获取最新的属性在项目列表中?
  • 编辑:

我可以使用以下命令加载xml:

<xmlproperty file="$myXMLFile.xml" collapseAttributes="true" keepRoot="false"/>

,通过添加以下目标,我可以扫描文件:

<target name="for-each" depends="compile">
<echo>for each test</echo>
<foreach list="${item.to}" target="loop" param="var" delimiter=","/>
</target>
<target name="loop">
<echo>inside loop</echo>
<echo message="To :: ${var}"/>
</target>

感谢

如果我知道你需要什么,也许你可以调整一下。

给定与示例类似的输入文件(source.xml):

<?xml version="1.0" encoding="UTF-8"?>
<items>
<item from="" to="not-last-to" version="first-one">
<subAttribute1>A</subAttribute1>
<subAttribute2>B</subAttribute2>
</item>
<item from="" to="not-last-to" version="second-one">
<subAttribute1>A2</subAttribute1>
<subAttribute2>B2</subAttribute2>
</item>
<item from="" to="last-to" version="last-one">
<subAttribute1>C</subAttribute1>
<subAttribute2>D</subAttribute2>
</item>
</items>

此构建文件:

<project name="ant" > 
<xslt in="source.xml" out="last-to.txt" style="style.xsl" /> 
<loadproperties srcFile="last-to.txt" />
<echo message="Last 'To' :: ${last-to}" />
</project> 

还有这个XSLT样式表,style。xsl(我拼凑在一起,不要认为这里有任何最佳实践!):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="text"/> 
<xsl:template match="item"> 
<xsl:apply-templates select="item"></xsl:apply-templates>
</xsl:template> 
<xsl:template match="item[last()]"> 
<xsl:apply-templates select="item">last-to:<xsl:value-of select="@to"/>
</xsl:apply-templates>
</xsl:template> 
</xsl:stylesheet>

运行Ant产生:

[echo] Last 'To' :: last-to

思路是使用XSLT样式表找出所需的元素,将其保存到Ant属性格式的文件中,然后将该文件读入构建中。

最新更新