
我昨天问了一个类似的问题,并得到了很好的答案.但是,我已经意识到这并没有涵盖我的所有基础,所以今天我要问一个更详细的问题.
XML IN
<?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>
HTML OUT
<!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 >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 ID="0001"><sup>1</sup></span></p> <div ID="note-0001" data-chunkID="a-01"> <p> You may need to consult a <span >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 >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 >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 >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但将每个块(xml:ID)保存在一起.我想避免divits(过度使用div),因此将div中的每个p包装出来,但我也试图避免无效的HTML.例如,很容易取父p(xml:ID = a-01)并将其包裹它的后代,然而,块级< div>另一个< p>将无效,浏览器将在文本结束后将所有内容整合为孤立文本.
我已经尝试过question from yesterday的各种改进的XSLT.然而,我发现自己处于一个不熟悉的领域.我也会对解决方案的简要解释有所帮助,因此我可以开始更好地理解XSLT,因为看起来我将在未来几个月花更多时间使用它.我应该拿起Michael Kay的书或其他东西.
编辑:我正在使用的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 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 > <xsl:apply-templates/> </span> </xsl:when> <xsl:when test="@rend[.= 'bold']"> <span > <xsl:apply-templates/> </span> </xsl:when> <xsl:otherwise> <span > <xsl:apply-templates/> </span> </xsl:otherwise> </xsl:choose></xsl:template>解决方法 看起来您的输入与输出有点不一致. (这是预期的输出,还是你现在得到的输出)? Chunks a-02和a-03没有< highlight>输入中的元素,但输出有< span class =“highlight ...”>元素.此外,块a-03在块引用后复制了文本. 我相信我已经制作了一个可行的解决方案来完成您示例中的所有 *** 作.你能试一试吗?
<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 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 > <xsl:apply-templates mode="contentOutput"/> </span> </xsl:template></xsl:stylesheet> 我相信unclosed Meta标签是使用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 >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 ID="0001"> <sup>1</sup> </span></p> <div ID="note-0001" data-chunkID="a-01"> <p> You may need to consult a <span >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,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”并手动将元素添加到转换,您可以获得相同的结果,但使用以下< head>
<head> <Meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" /> <Title>Test</Title> </head>总结
以上是内存溢出为你收集整理的html – 如果节点有某些子节点,则拆分它全部内容,希望文章能够帮你解决html – 如果节点有某些子节点,则拆分它所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)