gpt4 book ai didi

XSLT:将以下标题标签的同级移动到单独的主题中

转载 作者:行者123 更新时间:2023-12-03 15:49:31 27 4
gpt4 key购买 nike

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

我看到了这个 post 解决了类似的问题,但我的知识不足以修改它以在其他嵌套元素之前关闭 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>

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

最佳答案

这是我在递归函数中使用 XSLT 2.0 和 for-each-group group-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>

关于XSLT:将以下标题标签的同级移动到单独的主题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17476297/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com