如果节点有某些子节点,则拆分该节点

时间:2012-12-12 20:42:38

标签: html xml html5 xslt xslt-2.0

更新:我想我现在已回答了大部分问题,但处理<pgBreak>除外。您可以在编辑

下的帖子末尾看到我的更新和当前XSLT

昨天我问了一个类似的问题,并得到了很好的答案。但是,我已经意识到这并没有涵盖我的所有基础,所以今天我要问一个更详细的问题。

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 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但将每个chunk(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 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>

0 个答案:

没有答案