【发布时间】:2011-03-01 21:45:46
【问题描述】:
我在这里进行了一些搜索,发现了一些与我的问题相关的问题,但我仍然遇到问题......(希望我可以添加一个新问题而不是评论现有问题...... )
<?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>
我想将 /section/para 放置在新创建的评论节点中,如下所示:
<?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>
只需删除我想要包含在评论标签中的 paras。我尝试在我链接到的问题中逐行浏览样式表......有什么想法吗? 谢谢, bp
【问题讨论】:
-
亲爱的坏人,这是个好问题,+1。请参阅我的答案以获得完整、简单、简短且容易的解决方案,该解决方案也可能比其他解决方案更有效,而不是使用密钥。 :)