我有以下 XML:
<items>
<item>
<locations>
<location>
<latitude>-100</latitude>
<longitude>10</longitude>
</location>
<location>
<latitude>10</latitude>
<longitude>10</longitude>
</location>
</locations>
</item>
<item>
<locations>
<location>
<latitude>10</latitude>
<longitude>10</longitude>
</location>
<location>
<latitude>10</latitude>
<longitude>10</longitude>
</location>
</locations>
</item>
<items>
我需要计算具有无效纬度或经度值的项目。
有效纬度介于 -90 和 90 之间。有效经度介于 -180 和 180 之间。
为了便于发布,让我们尝试让它计算大于 -90 的纬度。
我尝试了以下方法,但均无效:
count(//item[locations/location[number(latitude) > -90])
count(//item[locations/location[number(latitude)] > -90)
count(//item[locations/location/*[number(latitude) > -90])
count(//item[locations/location/*[number(latitude)] > -90)
count(//item[locations/location/*[number(name() = latitude)] > -90)
count(//item[locations/location/*number([name() = latitude]) > -90)
count(//item[number(deal/locations/location/*[name()=latitude]) > -90])
有人知道这是否可行吗?如果不是,谁能想到一个巧妙的解决方法?
提前谢谢大家。
最佳答案
我认为您需要使用小于,而不是大于 - 您希望纬度小于 -90。
例如运行这个 xslt
<xsl:template match="/">
<GoodItems>
<xsl:value-of select="count(//item[not(locations/location[number(latitude) < -90
or number(latitude) > 90
or number(longitude) < -180
or number(longitude) > 180])])" />
</GoodItems>
<LatTooSmall>
<xsl:value-of select="count(//item[locations/location[number(latitude) < -90]])" />
</LatTooSmall>
<LatTooBig>
<xsl:value-of select="count(//item[locations/location[number(latitude) > 90]])" />
</LatTooBig>
<LongTooSmall>
<xsl:value-of select="count(//item[locations/location[number(longitude) < -180]])" />
</LongTooSmall>
<LongTooBig>
<xsl:value-of select="count(//item[locations/location[number(longitude) > 180]])" />
</LongTooBig>
</xsl:template>
针对此测试用例 xml 文档:
<items>
<item>
<locations>
<location>
<latitude>10</latitude>
<longitude>10</longitude>
</location>
</locations>
</item>
<item>
<locations>
<location>
<latitude>-100</latitude>
<longitude>10</longitude>
</location>
</locations>
</item>
<item>
<locations>
<location>
<latitude>123</latitude>
<longitude>10</longitude>
</location>
</locations>
</item>
<item>
<locations>
<location>
<latitude>0</latitude>
<longitude>-200</longitude>
</location>
</locations>
</item>
<item>
<locations>
<location>
<latitude>0</latitude>
<longitude>500</longitude>
</location>
</locations>
</item>
</items>
返回以下内容:
<GoodItems>1</GoodItems>
<LatTooSmall>1</LatTooSmall>
<LatTooBig>1</LatTooBig>
<LongTooSmall>1</LongTooSmall>
<LongTooBig>1</LongTooBig>
如果您有 xslt 2,您也可以使用 abs(),尽管 Dimitre 在 1.0 中有一个解决方法 here
关于xml - XPath 查询比较结果在数值范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13195526/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll