3

我想获取所有节点的值,这些值是“名称”之后的名称。但似乎 Nant xmlpeek 只能从 xml 中获取一个节点。有没有其他方法可以获取“名称”之后的所有节点?

xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<QAEnvironment xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <user>
                <name value="clientA" />
        <name value="clientB" />
        <name value="clientC" />
        <name value="clientD" />
    </user>
</QAEnvironment>

==================================================== ==============================

南特脚本:

<xmlpeek
    file="A.xml"
    xpath="/x:QAEnvironment/x:user/x:name/@value"
    property="clientName"
    nodeindex="3">
     <namespaces>
        <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
    </namespaces> 
    <echo message="clientName: ${clientName}" />
</xmlpeek>

==================================================== ============================= 结果 - clientName: clientD (as nodindex = "3")

谢谢

4

3 回答 3

1

My only suggestion is a recursive "loop":

<target name="get.xml.nodes">
    <property name="counter" value="1" if="${not property::exists(counter)}" />
    <property name="counter" value="${counter + 1}" if="${property::exists(counter)}" />

    <property name="temp" value="${node}" if="${property::exists(node)}"/>

    <xmlpeek file="file.xml" nodeindex="${counter}" xpath="/node" property="node" failonerror="false" />

    <call target="get.xml.nodes" unless="${node == temp}" />
<target>

Like Yan said, I don't think you can technically do this and <foreach> doesn't actually support this. LoopItems can only be Files, Folders, Lines, or Strings.

于 2014-01-09T20:17:16.067 回答
1

据我所知,该<xmlpeek>任务并非旨在返回多个结果。根据其官方描述:

如果 XPath 表达式指定多个节点,则节点索引用于确定返回哪个节点的文本。

如果您有确定同名节点数量的可靠方法,您可以尝试<foreach>在这些节点上运行循环。否则,我想您唯一的选择是为此创建一个自定义 NAnt 任务。据我所知,nant.contrib 项目没有任何用于此目的的东西。

于 2013-06-27T08:58:52.667 回答
0

@sirdank 的答案是最优雅的,但不适用于 nAnt 0.85 及更高版本。鉴于此,这里是 Nant 和嵌入式 VB.net 的简单组合,可以实现相同的效果:

<target name="parseNodes">


   <script language="VB">
     <references> 
<include name="System.dll" /> 
<include name="System.Xml.dll" /> 
     </references> 
     <imports> 
       <import namespace="System.Text.RegularExpressions" /> 
       <import namespace="System.Xml" /> 
       <import namespace="System.Xml.XPath" /> 
     </imports> 
     <code><![CDATA[

public shared sub ScriptMain(thisProject as Project)
    Dim xmlDoc as new XmlDocument()
    Dim nodes as XmlNodeList
    Dim packageString as string

    xmlDoc.Load(thisProject.properties("path"))

    nodes = xmlDoc.GetElementsByTagName("myNode")

    for each node as XmlNode in nodes
        nodeString += package.Attributes.itemOf("someAttribute").value & ","    
    next

    thisProject.properties("nodes") = nodeString

 end sub

     ]]>
     </code>
   </script>


   <echo message="Nodes: ${nodes}" />       

<foreach item="String" in="${nodes}" delim="," property="node">
        <echo message="${node}" />
</foreach>


 </target>

请注意,如果您有一个大文件,您可能应该在 VB 代码中使用 XPath。

于 2015-12-03T22:19:16.827 回答