更新:我想我现在已经回答了这个问题的大部分,除了 <pgBreak> 的处理。 .你可以看到我的更新和当前 XSLT在这篇文章的末尾 EDIT
我昨天问了一个类似的问题,得到了很好的回答。但是,此后我意识到这并没有涵盖我的所有基础,所以我今天要问一个更详细的问题。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<pgBreak pgId="i"/>
<p xml:id="a-01">
<highlight rend="italic">Bacon ipsum dolor sit amet</highlight> bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
<note.ref id="0001"><super>1</super></note.ref>
<note id="0001">
<p>
You may need to consult a <highlight rend="italic">latin</highlight> butcher. Good Luck.
</p>
</note>
Pork loin <pgBreak pgId="01"/> ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket rump ham, tail
hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola short ribs swine
beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola bacon ham
hock pork hamburger fatback.
</p>
<p xml:id="a-02">
Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
<p xml:id="a-03">
Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue.
<quote>
<p> 1.
Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
<p> 2.
Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin <pgBreak pgId="02"/>turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
<p> 3.
Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
</quote>
</p>
</root>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Test</title>
</head>
<body>
<div id="pg-i">
Page i
</div>
<p data-chunkid="a-01">
<span class="highlight-italic">Bacon ipsum dolor sit amet</span>bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin
pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef
hamburger
bacon filet mignon pork chop tail.
<span class="noteRef" id="0001"><sup>1</sup></span></p>
<div id="note-0001" data-chunkid="a-01">
<p>
You may need to consult a <span class="highlight-italic">latin</span> butcher. Good Luck.
</p>
</div>
<p data-chunkid="a-01">
Pork loin
</p>
<div id="pg-01">
Page 01
</div>
<p data-chunkId="a-01">
ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket
rump ham, tail
hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola
short ribs swine
beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola
bacon ham
hock pork hamburger fatback.
</p>
<p data-chunkid="a-02"><span class="highlight-italic">Bacon ipsum dolor sit</span> amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin
pastrami t-
bone. Sirloin turducken short ribs <span class="highlight-bold">t-bone</span> andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
<p data-chunkid="a-03">
Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs
doner tri-tip
tongue.
</p>
<blockquote data-chunkid="a-03">
<p> 1.
Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
<p>2.
Tri-tip ground round <span class="highlight-italic">short ribs</span> capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin
</p>
</blockquote>
<div id="pg-02">
Page: 02
</div>
<blockquote data-chunkid="a-03">
</p>
turducken short ribs t-bone andouille strip steak pork loin corned beef
hamburger bacon filet mignon pork chop tail.
</p>
<p> 3.
Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
</blockquote>
<p data-chunkid="a-03">
Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs
doner tri-tip
tongue.
</p>
</body>
</html>
我想将 xml 转换为 html5,但将每个 block (xml:id) 放在一起。我想避免 divits(过度使用 div),所以将每个 p 包装在一个 div 中是不可行的,但我也试图避免无效的 HTML。例如,获取父 p (xml:id=a-01) 并将其包裹在其后代中很容易,但是 block 级别 <div>和另一个 <p>将无效,并且浏览器会将文本结尾后的所有内容解释为孤立文本。
我试过各种修改XSLT来 self 的 question from yesterday .然而,我发现自己处在一个有点陌生的领域。我也会受益于对解决方案的简明解释,这样我就可以开始更好地理解 XSLT,因为看起来我将在接下来的几个月里花更多的时间在它身上。我可能应该拿起迈克尔凯或其他人的书。
编辑:我正在使用的 XSLT 的当前版本
注意:我还没有尝试过分页符。另外,我无法获得 <meta>标签关闭....氧气 14 一直提示这个。
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="p[not((parent::note,.//p, .//div))]">
<p data-chunkID="{@xml:id}">
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="p[.//p, .//div]">
<xsl:for-each-group select="node()" group-adjacent="boolean((self::text(), self::note.ref,self::highlight))">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<p data-chunkID="{../@xml:id}">
<xsl:apply-templates select="current-group()"/>
</p>
</xsl:when>
<xsl:when test="self::p">
<p>
<xsl:apply-templates/>
</p>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="note.ref">
<span class="noteRef" id="{@id}">
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="super">
<sup>
<xsl:apply-templates/>
</sup>
</xsl:template>
<xsl:template match="note">
<div id="note-{@id}" data-chunkID="{../@xml:id}">
<p>
<xsl:apply-templates/>
</p>
</div>
</xsl:template>
<xsl:template match="quote">
<blockquote data-chunkID="{../@xml:id}">
<p>
<xsl:apply-templates/>
</p>
</blockquote>
</xsl:template>
<xsl:template match="highlight">
<xsl:variable name="class" select="concat(name(.),'-',string(@rend))"/>
<xsl:choose>
<xsl:when test="@rend[.= 'italic']">
<span class="{$class}">
<xsl:apply-templates/>
</span>
</xsl:when>
<xsl:when test="@rend[.= 'bold']">
<span class="{$class}">
<xsl:apply-templates/>
</span>
</xsl:when>
<xsl:otherwise>
<span class="{$class}">
<xsl:apply-templates/>
</span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
最佳答案
看起来您的输入与输出有点不一致。 (这是预期的输出,还是你现在得到的输出)? block a-02 和 a-03 没有 <highlight>输入中的元素,但输出有 <span class="highlight...">元素。此外, block a-03 在 block 引用之后有重复的文本。
我相信我已经制定了一个可行的解决方案,可以完成您示例中的所有操作。你能试试这个吗?
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="p | div">
<xsl:variable name="breaks" select="note | pgBreak | quote" />
<xsl:variable name="firstNonBreak" select="node()[count(. | $breaks) != count($breaks)][1]" />
<xsl:variable name="nonBreaksAfterBreak"
select="$breaks/following-sibling::node()[1][count(. | $breaks) != count($breaks)]" />
<xsl:apply-templates select="$breaks | $firstNonBreak | $nonBreaksAfterBreak" mode="sectChild" />
</xsl:template>
<!-- Template to output the chunk id attribute of a particular hierarchy -->
<xsl:template name="ChunkId">
<xsl:variable name="id" select="ancestor::*[../self::root]/@xml:id" />
<xsl:if test="$id">
<xsl:attribute name="data-chunkid">
<xsl:value-of select="$id"/>
</xsl:attribute>
</xsl:if>
</xsl:template>
<!-- Splitting types - notes, page breaks, quotes -->
<xsl:template match="pgBreak" mode="sectChild">
<div id="pg-{@pgId}">
<xsl:value-of select="concat('Page ', @pgId)"/>
</div>
</xsl:template>
<xsl:template match="quote | note" mode="sectChild">
<xsl:apply-templates />
</xsl:template>
<!-- Receives the first node of each block of content outside of the splitting types
and passes processing onto itself and siblings within its block-->
<xsl:template match="text() | highlight | note.ref | super" mode="sectChild">
<xsl:variable name="content">
<xsl:apply-templates select="." mode="buildContent" />
</xsl:variable>
<xsl:if test="normalize-space($content)">
<xsl:call-template name="Nest">
<xsl:with-param name="hierarchy" select="ancestor::*[not(self::root)]" />
<xsl:with-param name="content" select="$content" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Recursive template to output nodes from the top level down to content -->
<xsl:template name="Nest">
<xsl:param name="topLevel" select="true()"/>
<xsl:param name="hierarchy" />
<xsl:param name="content" />
<xsl:variable name="top" select="$hierarchy[1]" />
<xsl:variable name="remainder" select="$hierarchy[position() > 1]" />
<!-- If there's a quote or note yet to come, don't output tags until we get there -->
<xsl:variable name="skipTags" select="boolean($remainder[self::quote or self::note])" />
<!-- Recursive output is captured in a variable, to be output later in this template -->
<xsl:variable name="inside">
<xsl:if test="$hierarchy">
<xsl:call-template name="Nest">
<xsl:with-param name="topLevel" select="$topLevel and $skipTags" />
<xsl:with-param name="hierarchy" select="$remainder" />
<xsl:with-param name="content" select="$content" />
</xsl:call-template>
</xsl:if>
</xsl:variable>
<xsl:choose>
<xsl:when test="not($hierarchy)">
<xsl:copy-of select="$content" />
</xsl:when>
<xsl:when test="$top/self::quote">
<blockquote>
<xsl:call-template name="ChunkId" />
<xsl:copy-of select="$inside"/>
</blockquote>
</xsl:when>
<xsl:when test="$top/self::note">
<div id="note-{$top/@id}">
<xsl:call-template name="ChunkId" />
<xsl:copy-of select="$inside"/>
</div>
</xsl:when>
<xsl:when test="not($skipTags)">
<xsl:element name="{name($top)}">
<xsl:if test="$topLevel">
<xsl:call-template name="ChunkId" />
</xsl:if>
<xsl:copy-of select="$inside"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$inside"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="node()" mode="buildContent">
<xsl:if test="not(self::note or self::quote or self::pgBreak)">
<!-- output this node -->
<xsl:apply-templates select="self::node()[normalize-space(.)]" mode="contentOutput" />
<!-- pass processing onto next sibling -->
<xsl:apply-templates select="following-sibling::node()[1]" mode="buildContent" />
</xsl:if>
</xsl:template>
<!-- Bottom level content - text, note refs, superscript, highlight-->
<xsl:template match="text()" mode="contentOutput">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="note.ref" mode="contentOutput">
<span class="noteRef" id="{@id}">
<xsl:apply-templates mode="contentOutput"/>
</span>
</xsl:template>
<xsl:template match="super" mode="contentOutput">
<sup>
<xsl:apply-templates mode="contentOutput"/>
</sup>
</xsl:template>
<xsl:template match="highlight" mode="contentOutput">
<xsl:variable name="class" select="concat(name(.),'-',string(@rend))"/>
<span class="{$class}">
<xsl:apply-templates mode="contentOutput"/>
</span>
</xsl:template>
</xsl:stylesheet>
我相信未关闭的元标记是使用 method="html" 的结果.您可能需要使用 method="xml"获取封闭元标记。与 method="html" ,上面的转换从您的样本输入中产生以下输出:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
</head>
<body>
<p data-chunkid="a-01"><span class="highlight-italic">Bacon ipsum dolor sit amet</span> bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
<span class="noteRef" id="0001">
<sup>1</sup>
</span></p>
<div id="note-0001" data-chunkid="a-01">
<p>
You may need to consult a <span class="highlight-italic">latin</span> butcher. Good Luck.
</p>
</div>
<p data-chunkid="a-01">
Pork loin </p>
<div id="pg-01">Page 01</div>
<p data-chunkid="a-01"> ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket rump ham, tail
hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola short ribs swine
beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola bacon ham
hock pork hamburger fatback.
</p>
<p data-chunkid="a-02">
Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
<p data-chunkid="a-03">
Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip
tongue.
</p>
<blockquote data-chunkid="a-03">
<p>
Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
bone. Sirloin </p>
</blockquote>
<div id="pg-02">Page 02</div>
<blockquote data-chunkid="a-03">
<p>turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger
bacon filet mignon pork chop tail.
</p>
</blockquote>
</body>
</html>
通过将方法更改为“xml”并手动添加 meta元素转换,您可以获得相同的结果,但具有以下 <head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
关于xml - 如果节点有某些子节点,则拆分该节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848239/
我有一个对象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
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我知道我可以指定某些字段来使用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
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
啊,正则表达式有点困惑。我正在尝试删除字符串末尾所有可能的标点符号:ifstr[str.length-1]=='?'||str[str.length-1]=='.'||str[str.length-1]=='!'orstr[str.length-1]==','||str[str.length-1]==';'str.chomp!end我相信有更好的方法来做到这一点。有什么指点吗? 最佳答案 str.sub!(/[?.!,;]?$/,'')[?.!,;]-字符类。匹配这5个字符中的任何一个(注意,。在字符类中并不特殊)?-前一个字符或组
我想编写一个ruby脚本来递归复制目录结构,但排除某些文件类型。因此,给定以下目录结构:folder1folder2file1.txtfile2.txtfile3.csfile4.htmlfolder2folder3file4.dll我想复制这个结构,但不包含.txt和.cs文件。因此,生成的目录结构应如下所示:folder1folder2file4.htmlfolder2folder3file4.dll 最佳答案 您可以使用查找模块。这是一个代码片段:require"find"ignored_extensions=[".cs"
基本上,我只是试图在满足特定条件时停止程序运行其余行。unlessraw_information.firstputs"Noresultswerereturnedforthatquery"breakend然而,在程序运行之前我得到了这个错误:Invalidbreakcompileerror(SyntaxError)执行此操作的正确方法是什么? 最佳答案 abort("Noresultswerereturnedforthatquery")unlesscondition或unlessconditionabort("Noresultswer