2013-07-04 1 views
3

Я переводил эту полу-html, полу-xml-вики в DITA. Моя конечная цель - достичь гнездования в соответствии с заголовками h. В основном перемещайте узлы после заголовка узлов (например, h2) за пределами узла body и заверните их в topic/body. Уровень заголовка потенциально может снизиться до h6.XSLT: перемещение следующих сиблингов заголовков тегов в отдельные темы

Я видел это post, который решает аналогичную проблему, но я недостаточно осведомлен, чтобы изменить его, чтобы закрыть тег тела перед другими вложенными элементами.

HTML/XML

<topic id="pageTitle"> 
    <title>Page Title</title> 
    <body> 
     <p>some contents.</p> 
     <h2>heading title</h2> 
     <p>some more content under h heading</p> 

     <h3>sub-heading title</h3> 
     <p>some more content under sub heading</p> 
     <p>some more content under sub heading</p> 

     <h2>heading title</h2> 
     <p>some more content under h heading</p> 
    </body> 
</topic> 

Я хочу добиться гнездования в DITA

<topic id="pageTitle"> 
    <title>Page Title</title> 
    <body> 
     <p>some contents.</p> 
    </body> 

    <topic id="headingtitle"> 
     <title>heading title</title> 
     <body> 
     <p>some more content under h heading</p> 
     </body> 

     <topic id="sub-headingtitle"> 
     <title>sub-heading title</title> 
     <body> 
      <p>some more content under sub heading</p> 
      <p>some more content under sub heading</p> 
     </body> 
     </topic> 
    </topic> 

    <topic id="headingtitle"> 
     <title>heading title</title> 
     <body> 
     <p>some more content under h heading</p> 
     </body> 
    </topic> 
</topic> 

Обратите внимание: <body> тег закрывается до другой темы начать. Это стандарт DITA, эта тема не должна входить в тело другой темы.

Кроме того, если узел тела немедленно следует узлу заголовка, то узел тела будет удален.

Например, XML

<topic id="pageTitle"> 
    <title>Page Title</title> 
    <body> 
     <h2>heading title</h2> 
     <p>some more content under h heading</p> 
    </body> 
</topic> 

Гол

<topic id="pageTitle"> 
    <title>Page Title</title> 

    <topic id="headingtitle"> 
     <title>h2 heading</title> 
     <body> 
     <p>some more content under h heading</p> 
     </body> 
    </topic> 

</topic> 

ответ

4

Вот мое предложение с помощью XSLT 2.0 и for-each-groupgroup-starting-with в рекурсивной функции:

<xsl:stylesheet 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mf="http://example.com/mf" 
    exclude-result-prefixes="xs mf"> 

<xsl:output indent="yes"/> 

<xsl:function name="mf:get-id-sub" as="xs:string"> 
    <xsl:param name="level" as="xs:integer"/> 
    <xsl:sequence select="string-join(for $i in 3 to $level return 'sub-', '')"/> 
</xsl:function> 

<xsl:function name="mf:group" as="element()*"> 
    <xsl:param name="elements" as="element()*"/> 
    <xsl:param name="level" as="xs:integer"/> 
    <xsl:for-each-group select="$elements" group-starting-with="*[local-name() eq concat('h', $level)]"> 
    <xsl:choose> 
     <xsl:when test="not(self::*[local-name() eq concat('h', $level)])"> 
     <body> 
      <xsl:apply-templates select="current-group()"/> 
     </body> 
     </xsl:when> 
     <xsl:otherwise> 
     <topic id="{mf:get-id-sub($level)}headingtitle"> 
      <xsl:apply-templates select="."/> 
      <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/> 
     </topic> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:for-each-group> 
</xsl:function> 

<xsl:template match="@* | node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@* , node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="topic[@id = 'pageTitle']/body"> 
    <xsl:sequence select="mf:group(*, 2)"/> 
</xsl:template> 

<xsl:template match="h2 | h3 | h4 | h5 | h6"> 
    <title> 
    <xsl:apply-templates/> 
    </title> 
</xsl:template> 

</xsl:stylesheet> 

Она превращает

<topic id="pageTitle"> 
    <title>Page Title</title> 
    <body> 
     <h2>heading title</h2> 
     <p>some more content under h heading</p> 
    </body> 
</topic> 

в

<topic id="pageTitle"> 
    <title>Page Title</title> 
    <topic id="headingtitle"> 
     <title>heading title</title> 
     <body> 
     <p>some more content under h heading</p> 
     </body> 
    </topic> 
</topic> 

и

<topic id="pageTitle"> 
    <title>Page Title</title> 
    <body> 
     <p>some contents.</p> 
     <h2>heading title</h2> 
     <p>some more content under h heading</p> 

     <h3>sub-heading title</h3> 
     <p>some more content under sub heading</p> 
     <p>some more content under sub heading</p> 

     <h2>heading title</h2> 
     <p>some more content under h heading</p> 
    </body> 
</topic> 

в

<topic id="pageTitle"> 
    <title>Page Title</title> 
    <body> 
     <p>some contents.</p> 
    </body> 
    <topic id="headingtitle"> 
     <title>heading title</title> 
     <body> 
     <p>some more content under h heading</p> 
     </body> 
     <topic id="sub-headingtitle"> 
     <title>sub-heading title</title> 
     <body> 
      <p>some more content under sub heading</p> 
      <p>some more content under sub heading</p> 
     </body> 
     </topic> 
    </topic> 
    <topic id="headingtitle"> 
     <title>heading title</title> 
     <body> 
     <p>some more content under h heading</p> 
     </body> 
    </topic> 
</topic> 
Смежные вопросы