我在这里做了一些搜索,发现了一些与我的问题有关的问题,但是我仍然遇到了麻烦......(希望我没关系,米增加一个新的问题,而不是评论对现有..)将某些选择节点的兄弟节点移动到一个新的父节点中
<?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。查看我的答案,获得一个完整的,简单的,简短的和简单的解决方案,它可能比其他解决方案更有效,而不是使用密钥。 :) –