<xsl:template match="location">
<xsl:if test="not(preceding::location)">
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
</xsl:if>
<tr>
<td><xsl:value-of select=".//name"/></td>
<td><xsl:value-of select=".//city"/></td>
<td><xsl:value-of select=".//state"/></td>
<td><xsl:value-of select=".//zip"/></td>
<td><xsl:value-of select=".//countdy"/></td>
</tr>
<xsl:if test="not(following::location)">
</table>
</xsl:if>
</xsl:template>
是否有任何方法允许 XSLT 中的不匹配标记...或者是否有其他方法可以达到相同的预期效果?
最佳答案
正如 Dimitre 所说,在 XSLT 中没有办法允许不匹配的标签。不过不应该有不匹配标签的理由。
查看您的模板,您似乎正试图从所有 <location> 构建一个 html 表格XML 实例的元素。您试图在第一个 <location> 处打开表格并试图在最后关闭表格 <location> .
执行此操作的最简单方法是在更高级别(父/祖先)打开您的表,然后使用 <location> 填充该表数据。
这是一个包含 3 <location> 的示例 XML 文件小号:
<doc>
<location>
<name>name 1</name>
<city>city 1</city>
<state>state 1</state>
<zip>zip 1</zip>
<country>country 1</country>
</location>
<location>
<name>name 2</name>
<city>city 2</city>
<state>state 2</state>
<zip>zip 2</zip>
<country>country 2</country>
</location>
<location>
<name>name 3</name>
<city>city 3</city>
<state>state 3</state>
<zip>zip 3</zip>
<country>country 3</country>
</location>
</doc>
这是将创建表格的样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="doc">
<!--The table is inserted here.-->
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
<!--This is where we apply the templates to populate the rows.-->
<xsl:apply-templates select="location"/>
</table>
</xsl:template>
<!--This template populates the row(s).-->
<xsl:template match="location">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="city"/>
</td>
<td>
<xsl:value-of select="state"/>
</td>
<td>
<xsl:value-of select="zip"/>
</td>
<td>
<xsl:value-of select="country"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
这是输出:
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
<tr>
<td>name 1</td>
<td>city 1</td>
<td>state 1</td>
<td>zip 1</td>
<td>country 1</td>
</tr>
<tr>
<td>name 2</td>
<td>city 2</td>
<td>state 2</td>
<td>zip 2</td>
<td>country 2</td>
</tr>
<tr>
<td>name 3</td>
<td>city 3</td>
<td>state 3</td>
<td>zip 3</td>
<td>country 3</td>
</tr>
</table>
如果出于某种原因您需要创建 <table>在第一个<location> ,你仍然可以这样做。不过,它需要更多代码。
以下样式表产生与第一个样式表相同的输出:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/doc">
<xsl:apply-templates/>
</xsl:template>
<!--The table is created at the first location and
the first row is populated.-->
<xsl:template match="location[1]">
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Country</th>
</tr>
<xsl:call-template name="location-row"/>
<!--Here is where we apply the other template to populate the other rows.
Notice we use a "mode" to differentiate the template from the generic
"location" template.-->
<xsl:apply-templates select="following-sibling::location" mode="not-first"/>
</table>
</xsl:template>
<!--This template will output the other rows.-->
<xsl:template match="location" mode="not-first" name="location-row">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="city"/>
</td>
<td>
<xsl:value-of select="state"/>
</td>
<td>
<xsl:value-of select="zip"/>
</td>
<td>
<xsl:value-of select="country"/>
</td>
</tr>
</xsl:template>
<!--This generic template matches locations other than the first one.
Basically it is consuming it so we don't get duplicate output.-->
<xsl:template match="location"/>
</xsl:stylesheet>
关于html - XSLT 允许不匹配的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5602732/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
如何匹配未被反斜杠转义的平衡定界符对(其本身未被反斜杠转义)(无需考虑嵌套)?例如对于反引号,我试过了,但是转义的反引号没有像转义那样工作。regex=/(?!$1:"how\\"#expected"how\\`are"上面的正则表达式不考虑由反斜杠转义并位于反引号前面的反斜杠,但我愿意考虑。StackOverflow如何做到这一点?这样做的目的并不复杂。我有文档文本,其中包括内联代码的反引号,就像StackOverflow一样,我想在HTML文件中显示它,内联代码用一些spanMaterial装饰。不会有嵌套,但转义反引号或转义反斜杠可能出现在任何地方。
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle