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