0
你能帮我转换下面的代码片段吗?包装和移动子节点
<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>
UPDATE 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>
喜迈克尔,可惜事实并非如此。我准备了一个更复杂的例子及其结果。 – 2014-09-24 22:49:38