英文:
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:mode
和except
):
<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.)
<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>
If you can't use XSLT 3.0, here's a 1.0 version (without xsl:mode
and except
)...
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论