插入一个新的DITA正文元素

huangapple go评论49阅读模式
英文:

Inserting a new DITA body element

问题

<topic>
<title>我的标题</title>
==><body>
<para>这里放入介绍文本。</para>
  <section/>
  <section/>
==></body>
</topic>
英文:

Looking to transform an XML file to a valid DTD topic. To do this I need to insert a body element as shown.

<topic>
<title>My Title</title>
==><body>
<para>Intro text goes here.</para>
  <section/>
  <section/>
==></body>
</topic>

</details>


# 答案1
**得分**: 1

如果您需要将`body`元素作为`topic`的子元素添加,请添加一个与`topic`匹配的模板,并在那里添加该元素。

唯一略微复杂的部分是您希望`title`在`body`之外,因此您需要将其与其余子元素分开。

以下是XSLT 3.0示例(我还添加了一个`topic`的`id`属性,并将`para`更改为`p`以使其有效):

```xslt
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" doctype-public="-//OASIS//DTD DITA Topic//EN" doctype-system="topic.dtd"/>
    <xsl:strip-space elements="*"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="topic">
        <topic id="{generate-id()}">
            <xsl:apply-templates select="@*|title"/>
            <body>
                <xsl:apply-templates select="node() except title"/>
            </body>
        </topic>
    </xsl:template>

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

</xsl:stylesheet>

如果您无法使用XSLT 3.0,这里是一个1.0版本(不包括xsl:modeexcept):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" doctype-public="-//OASIS//DTD DITA Topic//EN" doctype-system="topic.dtd"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="topic">
        <topic id="{generate-id()}">
            <xsl:apply-templates select="@*|title"/>
            <body>
                <xsl:apply-templates select="node()[not(self::title)]"/>
            </body>
        </topic>
    </xsl:template>

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

</xsl:stylesheet>

Fiddle链接:http://xsltfiddle.liberty-development.net/3Nzb5iZ

英文:

If you need to add a body element as a child of topic, add a template that matches topic and add the element there.

The only slightly tricky part is that you want the title outside of body, so you'll have to separate that from the rest of the children.

Here's an XSLT 3.0 example. (I also added an id attribute to topic and changed para to p so it would be valid.)

&lt;xsl:stylesheet version=&quot;3.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output indent=&quot;yes&quot; doctype-public=&quot;-//OASIS//DTD DITA Topic//EN&quot; doctype-system=&quot;topic.dtd&quot;/&gt;
    &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
    
    &lt;xsl:mode on-no-match=&quot;shallow-copy&quot;/&gt;
    
    &lt;xsl:template match=&quot;topic&quot;&gt;
        &lt;topic id=&quot;{generate-id()}&quot;&gt;
            &lt;xsl:apply-templates select=&quot;@*|title&quot;/&gt;
            &lt;body&gt;
                &lt;xsl:apply-templates select=&quot;node() except title&quot;/&gt;
            &lt;/body&gt;
        &lt;/topic&gt;
    &lt;/xsl:template&gt;
    
    &lt;xsl:template match=&quot;para&quot;&gt;
        &lt;p&gt;
            &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
        &lt;/p&gt;
    &lt;/xsl:template&gt;
    
&lt;/xsl:stylesheet&gt;

If you can't use XSLT 3.0, here's a 1.0 version (without xsl:mode and except)...

&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output indent=&quot;yes&quot; doctype-public=&quot;-//OASIS//DTD DITA Topic//EN&quot; doctype-system=&quot;topic.dtd&quot;/&gt;
    &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
    
    &lt;xsl:template match=&quot;@*|node()&quot;&gt;
        &lt;xsl:copy&gt;
            &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
        &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
    
    &lt;xsl:template match=&quot;topic&quot;&gt;
        &lt;topic id=&quot;{generate-id()}&quot;&gt;
            &lt;xsl:apply-templates select=&quot;@*|title&quot;/&gt;
            &lt;body&gt;
                &lt;xsl:apply-templates select=&quot;node()[not(self::title)]&quot;/&gt;
            &lt;/body&gt;
        &lt;/topic&gt;
    &lt;/xsl:template&gt;
    
    &lt;xsl:template match=&quot;para&quot;&gt;
        &lt;p&gt;
            &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
        &lt;/p&gt;
    &lt;/xsl:template&gt;
    
&lt;/xsl:stylesheet&gt;

Fiddle: http://xsltfiddle.liberty-development.net/3Nzb5iZ

huangapple
  • 本文由 发表于 2023年5月10日 23:22:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定