且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XMLProperty中和XML元素检索的属性值,如果特定属性为present /套

更新时间:2023-01-21 16:05:08


  

有没有办法让地方只有当标志被设置为某个值?


块引用>

不与 XMLProperty中,不,因为这将始终混为一谈具有相同的标签名的值。但 xmltask 可以做你需要的东西,因为它支持XPath的全部功能:

 <的taskdef NAME =xmltask类名=com.oopsconsultancy.xmltask.ant.XmlTask​​>
  <类路径PATH =xmltask.jar/>
< /&的taskdef GT;< xmltask源=$ {} base.dir /build/my.xml>
  <副本路径=/根/富[@标志='123'或@标志='256'] / @位置
        财产=foo.location
        追加=真propertySeparator =,/>
< / xmltask>
<回声> $ {foo.location}< /回声><! - 版画,我们 - >

如果你绝对不能使用第三方的任务,那么我可能会用一个简单的XSLT来提取XML,你的的要到另一个文件只是位解决这个问题:

 <的xsl:样式的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform版本=1.0>
  < XSL:PARAM NAME =targetFlag/>  < XSL:模板名称=标识匹配=@ * |节点()>
    < XSL:复制>< XSL:申请模板选择=@ * |节点()/>< / XSL:复制>
  < / XSL:模板>  <的xsl:模板匹配=foo的>
    <的xsl:if测试=@标志= $ targetFlag>
      < XSL:呼叫模板名称=标识/>
    < / XSL:如果>
  < / XSL:模板>
< / XSL:样式>

XSLT 任务调用此

 < XSLT中=$ {} base.dir /build/my.xml走出=filtered.xml的风格=extract.xsl>
  < PARAM NAME =targetFlag前pression =123/>
< / XSLT>

这将创建 filtered.xml 只包含

 <根和GT;
        <富位置=中的标志=123/>
< /根>

(在空白模的变化),你可以以正常的方式加载此使用XMLProperty中。

My Xml looks like:

<root>
        <foo location="bar"/>
        <foo location="in" flag="123"/>
        <foo location="pak"/>
        <foo location="us" flag="256"/>
        <foo location="blah"/>
</root>

For foo xml element flag is optional attribute.

And when I say:

<xmlproperty file="${base.dir}/build/my.xml" keeproot="false"/>

 <echo message="foo(location) : ${foo(location)}"/>

prints all locations :

foo(location) : bar,in,pak,us,blah

Is there a way to get locations only if flag is set to some value?

Is there a way to get locations only if flag is set to some value?

Not with xmlproperty, no, as that will always conflate values that have the same tag name. But xmltask can do what you need as it supports the full power of XPath:

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask">
  <classpath path="xmltask.jar" />
</taskdef>

<xmltask source="${base.dir}/build/my.xml">
  <copy path="/root/foo[@flag='123' or @flag='256']/@location"
        property="foo.location"
        append="true" propertySeparator="," />
</xmltask>
<echo>${foo.location}</echo><!-- prints in,us -->

If you absolutely cannot use third-party tasks then I'd probably approach the problem by using a simple XSLT to extract just the bits of the XML that you do want into another file:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="targetFlag" />

  <xsl:template name="ident" match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <xsl:template match="foo">
    <xsl:if test="@flag = $targetFlag">
      <xsl:call-template name="ident" />
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Call this with the xslt task

<xslt in="${base.dir}/build/my.xml" out="filtered.xml" style="extract.xsl">
  <param name="targetFlag" expression="123" />
</xslt>

This will create filtered.xml containing just

<root>
        <foo location="in" flag="123"/>
</root>

(modulo changes in whitespace) and you can load this using xmlproperty in the normal way.