我了解 XPath 语法的工作原理,并且可以编写 Xpath 命令以从 XML 文件中提取某些信息。
我想将我的 XPath 命令转换为 XSLT 脚本,这样其他人就可以在 XML 文件上运行脚本来获得相同的输出。
例如
我有一个 XML 文件,假设如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<section id="109196796">
<master_information>
<shelf_identifier>
<identifier type="CodeX" type_id="2">LB1500605917</identifier>
<identifier type="Common Code" type_id="15">150060591</identifier>
</shelf_identifier>
<shelf_master>
<section_type>1</section_type>
<book_type>3</book_type>
</shelf_master>
</master_information>
</section>
<section id="109196798">
<master_information>
<shelf_identifier>
<identifier type="CodeX" type_id="2">LB0777775917</identifier>
<identifier type="Common Code" type_id="15">077777591</identifier>
</shelf_identifier>
<shelf_master>
<section_type>1</section_type>
<book_type>3</book_type>
</shelf_master>
</master_information>
</section>
<section id="109196800">
<master_information>
<shelf_identifier>
<identifier type="CodeX" type_id="2">LB2589165917</identifier>
<identifier type="Common Code" type_id="15">258916591</identifier>
</shelf_identifier>
<shelf_master>
<section_type>1</section_type>
<book_type>3</book_type>
</shelf_master>
</master_information>
</section>
</library>
如果我运行下面的 XPath 命令,
//identifier[@type='CodeX']
我得到输出:
LB1500605917
LB0777775917
LB2589165917
..这是预期的。现在,我尝试将 XPath 命令转换为 XSL 语法,如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="return">
<xsl:text>
</xsl:text> <!-- defined a line break -->
</xsl:variable>
<xsl:template match="//library"></xsl:template>
<xsl:template match="//section/master_information/shelf_identifier/identifier">
<xsl:value-of select="@type='CodeX'"/>
<xsl:value-of select="$return"/> <!-- this basically puts a line break -->
</xsl:template>
</xsl:stylesheet>
XSL 似乎是正确的。但是,不会生成任何输出。
我是 XSL/XSLT 的新手。我究竟做错了什么?
最佳答案
您可以做的是添加一个与您的 xpath 匹配相同节点的模板,然后输出值和换行符...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<!--The strip-space isn't completely necessary. I just always include it in my
default stylesheets. It strips whitespace. You can preserve whitespace with
xsl:preserve-space. See https://www.w3.org/TR/xslt#strip for more details.-->
<xsl:strip-space elements="*"/>
<!--Suppress output of text nodes by built-in templates.-->
<xsl:template match="text()"/>
<!--Match "indentifier" elements that contain a "type" attribute
with the value of "CodeX".-->
<xsl:template match="identifier[@type='CodeX']">
<!--Output the value of the current context ("identifier") concatenated with
a newline. ("
" is a hex entity reference. You could also use a decimal
reference (" ")). You could use either of these references as the value
of a variable too (or even declare it as an entity).
I use normalize-space() instead of . to clean up any additional spaces.
See https://www.w3.org/TR/xpath/#function-normalize-space for details.-->
<xsl:value-of select="concat(normalize-space(),'
')"/>
</xsl:template>
</xsl:stylesheet>
注意匹配 text() 的空模板。这是为了通过 XSLT 的 built-in template rules 抑制 text() 节点的输出。 .
另请注意,我没有在匹配中使用 //。这也是因为内置规则;它们默认允许递归处理。
关于xml - 如何将 Xpath 语法转换为 XSL/XSLT 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40681076/
我正在学习如何使用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还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack