Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT:将标题标记的以下内容移动到单独的主题中_Xslt_Xpath_Xslt 2.0_Dita - Fatal编程技术网

XSLT:将标题标记的以下内容移动到单独的主题中

XSLT:将标题标记的以下内容移动到单独的主题中,xslt,xpath,xslt-2.0,dita,Xslt,Xpath,Xslt 2.0,Dita,我正在将这个半html、半xml的wiki翻译成DITA。我的最终目标是根据h标题实现嵌套。基本上,将标题节点(例如h2)后的节点移动到body节点之外,并用topic/body将它们包装起来。标题级别可能会下降到h6 我看到这个解决了类似的问题,但我没有足够的知识来修改它以在其他嵌套元素之前关闭body标记 HTML/XML <topic id="pageTitle"> <title>Page Title</title> <body>

我正在将这个半html、半xml的wiki翻译成DITA。我的最终目标是根据h标题实现嵌套。基本上,将标题节点(例如h2)后的节点移动到
body
节点之外,并用
topic/body
将它们包装起来。标题级别可能会下降到
h6

我看到这个解决了类似的问题,但我没有足够的知识来修改它以在其他嵌套元素之前关闭body标记

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>
<topic id="pageTitle">
   <title>Page Title</title>
   <body>
      <h2>heading title</h2>
      <p>some more content under h heading</p>
   </body>
</topic>

页面标题
一些内容

标题标题 h标题下的更多内容

小标题标题 小标题下还有一些内容

小标题下还有一些内容

标题标题 h标题下的更多内容

我想在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>
<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>

页面标题
一些内容

标题标题 h标题下的更多内容

小标题标题 小标题下还有一些内容

小标题下还有一些内容

标题标题 h标题下的更多内容

请注意:在其他主题开始之前,
标记已关闭。这是DITA的标准,该主题不能嵌套在另一个主题的主体内

此外,如果主体节点后面紧跟着标题节点,则主体节点将被删除

例如,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>
<topic id="pageTitle">
   <title>Page Title</title>
   <body>
      <h2>heading title</h2>
      <p>some more content under h heading</p>
   </body>
</topic>

页面标题
标题标题
h标题下的更多内容

目标

<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>

页面标题
h2标题
h标题下的更多内容


以下是我的建议,在递归函数中为每个组使用XSLT 2.0和
组,从
开始:

<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>

页面标题
标题标题
h标题下的更多内容

进入


页面标题
标题标题
h标题下的更多内容


页面标题
一些内容

标题标题 h标题下的更多内容

小标题标题 小标题下还有一些内容

小标题下还有一些内容

标题标题 h标题下的更多内容

进入


页面标题
一些内容

标题标题 h标题下的更多内容

小标题标题 小标题下还有一些内容

小标题下还有一些内容

标题标题 h标题下的更多内容