编辑:对于那些将来会遇到这个问题的人来说,这是一个写得很糟糕的问题。这不是我所追求的。 这个问题也可能对你有用。
所以,我XSLT
这几天一直在努力复习。我对它非常陌生,过去的大部分时间都XQuery
用于转换我的 XML。我被困在一个相当简单的问题上,但是环顾四周,我还没有找到明确的解决方案。简单地说,我想根据它的子元素将一些元素分成两部分。
例如,如果我的 XML 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>
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.
<note.ref id="0001"><super>1</super></note.ref>
<note id="0001">
<p>
You may need to consult a latin butcher. Good Luck.
</p>
</note>
Pork loin 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>
</root>
在我运行我之后,我xsl
留下了如下内容:
<html>
<body>
<p>
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.
<span class="noteRef" id="0001"><sup>1</sup></span>
<div id="note-0001">
<p>
You may need to consult a latin butcher. Good Luck.
</p>
</div>
Pork loin 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>
</body>
</html>
这样做的问题显然是一个HTML
<p>
不能有一个<div>
作为孩子,让一个孤独的另一个<p>
作为孙子。这只是无效的。浏览器(例如 chromium)可能会在点击 时呈现第一段结尾<div>
,适当地将注释包装在它自己的<p>
中,但在注释之后留下文本 orpahened。这样应用到的任何 CSS<p>
都将无法应用。
如何<p>
根据元素后代将一个元素分成两个?
期望的输出
<html>
<body>
<p>
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.
<span class="noteRef" id="0001"><sup>1</sup></span><
</p>
<div id="note-0001">
<p>
You may need to consult a latin butcher. Good Luck.
</p>
</div>
<p>
Pork loin 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>
</body>
</html>
我已经稍微抽象了我的问题,所以XSL
我尝试过的以下内容可能会略有偏差。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xs xd" version="2.0">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="p">
<p>
<xsl:apply-templates/>
</p>
</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}">
<xsl:apply-templates/>
</div>
</xsl:template>
</xsl:stylesheet>