如果我有这个 XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:template match='/'>
<xsl:value-of select="//Description" />
</xsl:template>
</xsl:stylesheet>
还有这个 XML
<ArrayOfLookupValue xmlns="http://switchwise.com.au/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<LookupValue>
<Description>AGL</Description>
<Value>8</Value>
</LookupValue>
<LookupValue>
<Description>Australian Power & Gas</Description>
<Value>6</Value>
</LookupValue>
<LookupValue>
<Description>EnergyAustralia</Description>
<Value>13</Value>
</LookupValue>
<LookupValue>
<Description>Origin Energy</Description>
<Value>9</Value>
</LookupValue>
<LookupValue>
<Description>TRU Energy</Description>
<Value>7</Value>
</LookupValue>
</ArrayOfLookupValue>
我实际上如何从这一行中获取一些数据:
<xsl:value-of select="//Description" />
我在这上面花了几个小时,我得出的结论是 xmlns= 命名空间是让我感到悲伤的原因。
非常感谢任何帮助。
顺便说一句,XML 来自 Web 服务,所以我不能只是“更改”它 - 我可以预处理它,但这并不理想...
我还确认从模拟的 XML 中删除命名空间确实可以解决问题。
最佳答案
这是关于 XPath 和 XSLT 的最多的常见问题解答。
简短的回答是,在 XPath 中,没有前缀的名称被认为属于“无命名空间”。但是,在具有默认命名空间的文档中,未加前缀的名称属于默认命名空间。
因此,对于这样的文档表达式
//Description
不选择任何内容(因为文档中没有Description(或任何其他)元素属于“无命名空间”——所有元素名称都属于默认值命名空间)。
解决方案:
在 XSLT 中定义一个命名空间,它与 XML 文档的默认命名空间具有相同的 namespace-uri()。然后将如此定义的命名空间的前缀用于 Xpath 表达式中使用的任何名称:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://switchwise.com.au/">
<xsl:output method="html"/>
<xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:template match='/'>
<xsl:copy-of select="//x:Description" />
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<ArrayOfLookupValue xmlns="http://switchwise.com.au/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<LookupValue>
<Description>AGL</Description>
<Value>8</Value>
</LookupValue>
<LookupValue>
<Description>Australian Power & Gas</Description>
<Value>6</Value>
</LookupValue>
<LookupValue>
<Description>EnergyAustralia</Description>
<Value>13</Value>
</LookupValue>
<LookupValue>
<Description>Origin Energy</Description>
<Value>9</Value>
</LookupValue>
<LookupValue>
<Description>TRU Energy</Description>
<Value>7</Value>
</LookupValue>
</ArrayOfLookupValue>
产生了想要的、正确的结果:
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>AGL</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>Australian Power & Gas</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>EnergyAustralia</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>Origin Energy</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>TRU Energy</Description>
关于xml - xsl命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7155891/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。