本文介绍了xslt将子级属性移动到父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:

    <book>
     <chapter href="..">
      <topicref chunk="to-content" href"..">

      </topicref>
      <topicref chunk="to-content" href"..">

      </topicref>
     </chapter>
    </book>

输出:

    <book>
     <chapter chunk="to-content" href="..">
      <topicref href"..">

      </topicref>
      <topicref href"..">

      </topicref>
     </chapter>
    </book>

我不能使用xsl:attribute name="chunk">to-content</xsl:attribute>,因为它会抛出如果先前的指令创建了任何子代,则在此处创建属性将失败".警告,然后出错.我了解,如此处所述.有什么解决方法吗?

I cannot use xsl:attribute name="chunk">to-content</xsl:attribute> because it throws "creating an attribute here will fail if previous instructions create any children." warning and then error. I understand that as described here. Any workaround?

将XSLT 2.0与Saxon 9配合使用(仍然使XSLT/S.O.挂起).抱歉,如果范围太广,但是任何方向的帮助,我们将不胜感激.

Using XSLT 2.0 with Saxon 9. (just getting the hang of XSLT/ S.O. still). Sorry if this is too broad but any help in any direction will be appreciated.

推荐答案

为了向chapter元素添加属性,最好具有与chapter元素匹配的模板-遵循以下原则:

In order to add an attribute to the chapter element, it would be best to have a template matching the chapter element - along the lines of:

<xsl:template match="chapter">
    <xsl:copy>
        <xsl:attribute name="chunk">to-content</xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

类似地,要从topicref中删除chunk属性:

Similarly, to remove the chunk attribute from topicref:

<xsl:template match="topicref/@chunk"/>

这篇关于xslt将子级属性移动到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:00