你能帮我转换下面的片段吗?
<root>
<topic> <!-- First topic -->
<p>content1</p>
<topic> <!-- Second topic -->
<p>content2</p>
</topic>
<topic> <!-- Third topic -->
<p>content3</p>
</topic>
</topic>
</root>
我需要从第一个主题中删除第二个主题和第三个主题,将它们包含在新 / 空主题中,并将新主题与其子主题相关联根节点。
<root>
<topic> <!-- First topic -->
<p>content1</p>
</topic>
<topic> <!-- New topic -->
<topic> <!-- Second topic -->
<p>content2</p>
</topic>
<topic> <!-- Third topic -->
<p>content3</p>
</topic>
</topic>
</root>
也许有一个非常简单的解决方案。我试图在第二个/后缀第三个主题前加上一个标签(对我来说看起来像一个非常脏的解决方案),但我无法移动它们。
<xsl:if test="position() = 1">
<![CDATA[
<topic>
]]>
...
</xsl:if>
<xsl:if test="position() = last()">
...
<![CDATA[
</topic>
]]>
</xsl:if>
更新1 这是一个更复杂和更详细的例子:
来源 - 转型前
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!--
The root has only a
title and multiple child
topics. but no other child
elements.
-->
<title>Root</title>
<topic id="topic_1">
<!--
Allowed child elements of 'topic'
are listed in the DITA spec.:
http://bit.ly/1ruYbdq
-->
<title>First Topic - First Level</title>
</topic>
<topic id="topic_2">
<title>Second Topic - First Level</title>
<!--
This is the main problem.
A topic must not contain
child topics AND other child
elements after the
transformation.
If a topic has child topic
AND other child elements, the
topics have to be extracted.
-->
<topic id="topic_3">
<title>Third Topic - Second Level</title>
</topic>
<topic id="topic_4">
<!--
The number of topics is not limited.
-->
<title>Fourth Topic - Second Level</title>
<topic id="topic_5">
<!--
Third level topics have to
be moved to the second
hierarchy level. No topic
may reside on the third
level after transformation.
-->
<title>Fifth Topic - Third Level</title>
</topic>
</topic>
</topic>
</root>
结果 - 转型后
<?xml version="1.0" encoding="UTF-8"?>
<root>
<title>Root</title>
<topic id="topic_1">
<title>First Topic - First Level</title>
</topic>
<topic id="topic_2">
<title>Second Topic - First Level</title>
</topic>
<!--
The third and fourth topic have
been moved extracted from the
second topic. Both (could be any
number) have been wrapped with a
dummy 'topic' element.
-->
<topic>
<!--
The second level topics have
been wrapped with a "dummy"
topic element.
-->
<topic id="topic_3">
<title>Fourth Topic - Second Level</title>
</topic>
<topic id="topic_4">
<title>Fifth Topic - Second Level</title>
</topic>
<topic id="topic_5">
<!--
The third level topic
has been moved to the
second hierarchy level.
-->
<title>Sixth Topic - Third Level</title>
</topic>
</topic>
</root>
答案 0 :(得分:0)
也许有一个非常简单的解决方案。
也许有,但很难看到这里给出的是什么,只是一个例子。这个非常简单的解决方案对你有用吗?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<xsl:copy>
<topic>
<xsl:copy-of select="topic/p"/>
</topic>
<topic>
<xsl:copy-of select="topic/topic"/>
</topic>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
好的,这对我有用:
<xsl:template match="//topic">
<xsl:choose>
<xsl:when test="topic[not(topic)]">
<!--
Wrap first level topics in a
single <section> element.
-->
<section>
<xsl:apply-templates/>
</section>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<!--
Wrap nested topics in a
<section> container.
-->
<xsl:when test="(count(preceding-sibling::topic) = 0) and (count(following-sibling::topic) >= 1)">
<!--
Close the section of the parent <topic> element.
-->
<xsl:text disable-output-escaping="yes"></section></xsl:text>
<xsl:text disable-output-escaping="yes"><section></xsl:text>
<section>
<xsl:apply-templates/>
</section>
</xsl:when>
<xsl:otherwise>
<section>
<xsl:apply-templates/>
</section>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>