XMLProperty中和XML元素检索的属性值,如果特定属性为present /套 [英] xmlproperty and retrieving property value of xml element if specific attribute is present/set

查看:117
本文介绍了XMLProperty中和XML元素检索的属性值,如果特定属性为present /套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML看起来像:

 <根和GT;
        <富位置=酒吧/>
        <富位置=中的标志=123/>
        <富位置=小唐/>
        <富位置=US标记=256/>
        <富位置=嗒嗒/>
< /根>

有关foo的XML元素标志是可选的属性。

当我说:

 < XMLProperty中的文件=$ {} base.dir /build/my.xmlkeeproot =FALSE/> <回声消息=富(位置):$ {foo的(位置)}/>

打印所有位置:

 富(位置):酒吧,,小唐,美国,等等

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


解决方案

  

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


不与 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.

这篇关于XMLProperty中和XML元素检索的属性值,如果特定属性为present /套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆