2011-03-01 77 views
1

我在这里做了一些搜索,发现了一些与我的问题有关的问题,但是我仍然遇到了麻烦......(希望我没关系,米增加一个新的问题,而不是评论对现有..)将某些选择节点的兄弟节点移动到一个新的父节点中

<?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

+0

亲爱的坏人,这是一个很好的问题+1。查看我的答案,获得一个完整的,简单的,简短的和简单的解决方案,它可能比其他解决方案更有效,而不是使用密钥。 :) –

回答

3

这就是分组邻接para元素。

问题出在Conflict Resolution for Template Rules:因为两个para匹配规则具有相同的导入优先级和相同的计算优先级,错误恢复机制可能导致应用最后一个。

你需要像“第一para儿”规则明确定义@priority

<xsl:template match="section[para][section]/para[1]" priority="1"> 
</ 

有了这样的修改,输出为:

<?xml version="1.0" encoding="UTF-16"?> 
<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> 
+0

这工作完美,谢谢! – badperson

+0

+1。正确答案。 – Flack

1

我想这样做

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="kPreceding" match="para" 
    use="generate-id(following-sibling::section[1])"/> 

<xsl:template match="node()|@*" name="identity"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="section[preceding-sibling::para]"> 
    <comment> 
    <xsl:apply-templates mode="copy" 
     select="key('kPreceding', generate-id())"/> 
    </comment> 

    <xsl:call-template name="identity"/> 
</xsl:template> 

<xsl:template match= 
    "para[following-sibling::section]"/> 

<xsl:template match="para" mode="copy"> 
    <xsl:call-template name="identity"/> 
</xsl:template> 
</xsl:stylesheet> 

当该变换被应用所提供的XML文档:

<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> 
    <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> 

说明

  1. 的身份规则(模板)每n次复制一次颂歌“原样”。

  2. section的首要模板具有前述兄弟(一个或多个)包裹para所有这样的兄弟姐妹与comment元件,然后调用本身的恒等变换。

  3. 为了方便起见,我们定义了一个键,该键匹配section元素前面的所有para元素与给定的generate-id()

  4. 具有以下兄弟sectionpara元素通过覆盖模板排除在身份规则的操作之外,该操作不会执行任何操作。

  5. 最后,这样para元素,当包装comment内被输出在模式copy,这只是简单地调用身份规则做复制进行处理。

+0

+1这将是另一个可能的样式表。 – 2011-03-02 12:29:21

相关问题