我在這裏做了一些搜索,發現了一些與我的問題有關的問題,但是我仍然遇到了麻煩......(希望我沒關係,米增加一個新的問題,而不是評論對現有..)將某些選擇節點的兄弟節點移動到一個新的父節點中
<?xml version="1.0"?>
<section>
<title>Main Section</title>
<para>Here is some text that is a child of a main section.</para>
<para>Some more text.</para>
<para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
我想有/節/第置於一個新創建的註釋節點內,像這樣:
<?xml version="1.0"?>
<section>
<title>Main Section</title>
<comment>
<para>Here is some text that is a child of a main section.</para>
<para>Some more text.</para>
<para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para>
</comment>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
我嘗試了一些我發現搜索stackoverflow的建議,最接近的是here.
這是我使用的樣式表:
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- paras that are children of a section that has direct para children and dircect section children -->
<xsl:template match="section[para][section]/para[1]">
<comment>
<xsl:apply-templates select="../para" mode="commentPara"/>
</comment>
</xsl:template>
<xsl:template match="*" mode="commentPara">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="section[para][section]/para"/>
</xsl:stylesheet>
它輸出這樣的:
<?xml version='1.0' ?>
<section>
<title>Main Section</title>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
簡單地刪除我想在一個註釋標記來包裝段。我試着基本上一行一行地通過問題中的樣式錶鏈接到...任何想法? 謝謝, bp
親愛的壞人,這是一個很好的問題+1。查看我的答案,獲得一個完整的,簡單的,簡短的和簡單的解決方案,它可能比其他解決方案更有效,而不是使用密鑰。 :) –