我要选择所有节点<cci:p> 不有属性。
因此,在下面的示例中,有一个节点(用粗体文本或 ** 表示)导致了一些问题。基本上,我想选择所有 <cci:p>节点并将它们包裹在 <p> 中输出标签。但是这个节点导致输出了一个额外的段落,这是不正确的。在这种情况下,我希望发生的是,如果找到具有该属性的节点,我想将其附加到先前处理的节点。
这是我得到的:
加利福尼亚州奥克兰 ���� 一名前学生涉嫌向一名小基督徒开火 加利福尼亚州的一所大学,造成七人死亡,三人受伤,目标是一所学校 警方称,他认为管理员和以前的同学对他不公平 昨天。
奥克兰警察局长霍华德乔丹在新闻发布会上说,43 岁的 One Goh 曾被 Oikos 大学开除,曾与调查人员合作
在被拘留后,但...并不是特别后悔。����我们知道他来这里的目的是寻找管理员,而她是 不在这里,”乔丹说。 ����然后他系统地检查了整个建筑物并 随机射击受害者。
这是我想要得到的:
加利福尼亚州奥克兰 ���� 一名前学生涉嫌向一名小基督徒开枪 加利福尼亚州的一所大学,造成七人死亡,三人受伤,目标是一所学校 警方称,他认为管理员和以前的同学对他不公平 昨天。
奥克兰警察局长霍华德乔丹在新闻发布会上说,43 岁的 One Goh 曾被 Oikos 大学开除,曾与调查人员合作 在被拘留后,但...并不是特别懊悔。
����我们知道他来这里的目的是寻找管理员,而她是 不在这里,”乔丹说。 ����然后他系统地检查了整个建筑物并 随机射击受害者。
示例 XML:
<cci:body class="element" displayname="body" name="body">
<cci:p>OAKLAND, Calif. — A former student suspected of opening fire at a small Christian college in California, killing seven people and wounding three, was targeting a school administrator and former classmates who he felt had treated him unfairly, police said yesterday.</cci:p>
<cci:p>Oakland Police Chief Howard Jordan said at a news conference that One Goh, 43, who had been expelled from Oikos University, had been cooperative with investigators </cci:p>
**<cci:p ccix:annotation="insertion">after being taken into custody but “not particularly remorseful.”</cci:p>**
<cci:p>“We know that he came here with the intent of locating an administrator, and she was not here,” Jordan said. “He then went through the entire building systematically and randomly shooting victims.”</cci:p>
<cci:p>The midmorning attack at Oikos, a small Oakland college that has links to the Korean-American Christian community, was the deadliest shooting rampage on a U.S. college campus since </cci:p>
</cci:body>
示例 XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cci="urn:schemas-ccieurope.com"
xmlns:ccit="http://www.ccieurope.com/xmlns/ccimltables" xmlns:ccix="http://www.ccieurope.com/xmlns/ccimlextensions"
exclude-result-prefixes="xsl cci ccit ccix">
<xsl:strip-space elements="*" />
<xsl:output method="html" encoding="UTF-8" />
<xsl:template match="/">
<html>
<xsl:apply-templates />
</html>
</xsl:template>
<xsl:template match="cci:p">
<xsl:choose>
<xsl:when test="@ccix:annotation='insertion'">
<xsl:apply-templates />
</xsl:when>
<xsl:otherwise>
<p>
<xsl:apply-templates />
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="cci:italic">
<em>
<xsl:apply-templates />
</em>
</xsl:template>
<xsl:template match="cci:endnote_contrib">
<em>
<xsl:apply-templates />
</em>
</xsl:template>
<xsl:template match="cci:extra_leading">
</xsl:template>
<xsl:template match="cci:bold">
<strong>
<xsl:apply-templates />
</strong>
</xsl:template>
<xsl:template match="cci:subhead">
<h2 class="cci-subhead">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="ccit:table">
<table class="cci-table">
<xsl:apply-templates />
</table>
</xsl:template>
<xsl:template match="ccit:tr">
<tr>
<xsl:apply-templates />
</tr>
</xsl:template>
<xsl:template match="ccit:td">
<td>
<xsl:value-of select="." />
</td>
</xsl:template>
<xsl:template match="cci:l_category">
<h2 class="cci-category">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_category_sub">
<h2 class="cci-category-sub">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_region">
<h2 class="cci-region">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_region_location">
<h2 class="cci-region-location">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_region_sub">
<h2 class="cci-region-sub">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="factbox_bold">
<strong>
<xsl:apply-templates />
</strong>
</xsl:template>
<xsl:template match="cci:factbox_head">
<strong>
<xsl:value-of select="." />
</strong>
</xsl:template>
<xsl:template match="cci:z_sym_round_bullet">
•
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="cci:z_sym_triangle_bullet">
•
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
最佳答案
我会定义一个键来将具有该属性的元素映射到您要插入它们的前一个兄弟:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cci="urn:schemas-ccieurope.com"
xmlns:ccit="http://www.ccieurope.com/xmlns/ccimltables" xmlns:ccix="http://www.ccieurope.com/xmlns/ccimlextensions"
exclude-result-prefixes="xsl cci ccit ccix">
<xsl:key name="k1" match="cci:p[@ccix:annotation = 'insertion']"
use="generate-id(preceding-sibling::cci:p[not(@ccix:annotation)][1])"/>
<xsl:strip-space elements="*" />
<xsl:output method="html" encoding="UTF-8" />
<xsl:template match="/">
<html>
<xsl:apply-templates />
</html>
</xsl:template>
<xsl:template match="cci:p[not(@ccix:annotation)]">
<p>
<xsl:apply-templates select="node() | key('k1', generate-id())/node()"/>
</p>
</xsl:template>
<xsl:template match="cci:p[@ccix:annotation = 'insertion']"/>
<xsl:template match="cci:italic">
<em>
<xsl:apply-templates />
</em>
</xsl:template>
<xsl:template match="cci:endnote_contrib">
<em>
<xsl:apply-templates />
</em>
</xsl:template>
<xsl:template match="cci:extra_leading">
</xsl:template>
<xsl:template match="cci:bold">
<strong>
<xsl:apply-templates />
</strong>
</xsl:template>
<xsl:template match="cci:subhead">
<h2 class="cci-subhead">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="ccit:table">
<table class="cci-table">
<xsl:apply-templates />
</table>
</xsl:template>
<xsl:template match="ccit:tr">
<tr>
<xsl:apply-templates />
</tr>
</xsl:template>
<xsl:template match="ccit:td">
<td>
<xsl:value-of select="." />
</td>
</xsl:template>
<xsl:template match="cci:l_category">
<h2 class="cci-category">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_category_sub">
<h2 class="cci-category-sub">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_region">
<h2 class="cci-region">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_region_location">
<h2 class="cci-region-location">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="cci:l_region_sub">
<h2 class="cci-region-sub">
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="factbox_bold">
<strong>
<xsl:apply-templates />
</strong>
</xsl:template>
<xsl:template match="cci:factbox_head">
<strong>
<xsl:value-of select="." />
</strong>
</xsl:template>
<xsl:template match="cci:z_sym_round_bullet">
•
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="cci:z_sym_triangle_bullet">
•
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
使用该样式表 Saxon 6.5.5 输出结果
<html>
<p>OAKLAND, Calif. — A former student suspected of opening fire at a small Christian college in California, killing seven people
and wounding three, was targeting a school administrator and former classmates who he felt had treated him unfairly, police
said yesterday.
</p>
<p>Oakland Police Chief Howard Jordan said at a news conference that One Goh, 43, who had been expelled from Oikos University,
had been cooperative with investigators after being taken into custody but “not particularly remorseful.”
</p>
<p>“We know that he came here with the intent of locating an administrator, and she was not here,” Jordan said. “He then went
through the entire building systematically and randomly shooting victims.”
</p>
<p>The midmorning attack at Oikos, a small Oakland college that has links to the Korean-American Christian community, was the
deadliest shooting rampage on a U.S. college campus since
</p>
</html>
输入
<cci:body class="element" displayname="body" name="body" xmlns:cci="urn:schemas-ccieurope.com" xmlns:ccix="http://www.ccieurope.com/xmlns/ccimlextensions">
<cci:p>OAKLAND, Calif. — A former student suspected of opening fire at a small Christian college in California, killing seven people and wounding three, was targeting a school administrator and former classmates who he felt had treated him unfairly, police said yesterday.</cci:p>
<cci:p>Oakland Police Chief Howard Jordan said at a news conference that One Goh, 43, who had been expelled from Oikos University, had been cooperative with investigators </cci:p>
<cci:p ccix:annotation="insertion">after being taken into custody but “not particularly remorseful.”</cci:p>
<cci:p>“We know that he came here with the intent of locating an administrator, and she was not here,” Jordan said. “He then went through the entire building systematically and randomly shooting victims.”</cci:p>
<cci:p>The midmorning attack at Oikos, a small Oakland college that has links to the Korean-American Christian community, was the deadliest shooting rampage on a U.S. college campus since </cci:p>
</cci:body>
关于xml - XSLT - 选择没有属性的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076331/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个对象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
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow