首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在同一元素中移动“以下”元素时复制元素

在同一元素中移动“以下”元素时复制元素
EN

Stack Overflow用户
提问于 2018-01-07 18:28:11
回答 1查看 26关注 0票数 0

在下面这个基于文本的XML示例中,使用XSL3.0/Saxon,我希望将任何紧跟在seg元素后面的seg元素移动到seg元素中,就在关闭</seg>之前,以及在seg中任何文本()和子元素之后。

我在http://xsltfiddle.liberty-development.net/pPgCconhttp://xsltfiddle.liberty-development.net/pPgCcon/1的实验(a)不雅,(b)未能捕捉到正确的following::元素。

是源XML:

代码语言:javascript
复制
<deposition>
    <deposition-title>Praesent vitae</deposition-title>
    <text>
        <seg n="seg1" type="not_foo">Lorem ipsum dolor sit amet, consectetur 
            adipiscing elit. Vivamus<note2 n="abc">another note 2</note2><appnote>a</appnote> ultrices consequat facilisis. 
            Suspendisse a odio<note n="def">foo note</note><footnote>1</footnote> in lobortis. Aenean 
            non dui scelerisque, rutrum est at, cursus sem.</seg>
        <seg n="seg2" type="foo">Ut pharetra bibendum ipsum, portitor 
            velit pharetra quis. Aeneano<note n="ghi">foo note</note><footnote>2</footnote> purus. Praesent 
            aliquam viverra tellus<note n="jkl">another note</note><footnote>3</footnote> in condimentum.</seg><footnote>4</footnote>
    </text>
<deposition>

这就是目标XML --注意<footnote>4</footnote>是如何从前面的seg外部移动到seg元素中的,在完成了text()之后。

代码语言:javascript
复制
<deposition>
    <deposition-title>Praesent vitae</deposition-title>
    <text>
        <seg n="seg1" type="not_foo">Lorem ipsum dolor sit amet, consectetur 
            adipiscing elit. Vivamus<note2 n="abc">another note 2</note2><appnote>a</appnote> ultrices consequat facilisis. 
            Suspendisse a odio<note n="def">foo note</note><footnote>1</footnote> in lobortis. Aenean 
            non dui scelerisque, rutrum est at, cursus sem.</seg>
        <seg n="seg2" type="foo">Ut pharetra bibendum ipsum, portitor 
            velit pharetra quis. Aeneano<note n="ghi">foo note</note><footnote>2</footnote> purus. Praesent 
            aliquam viverra tellus<note n="jkl">another note</note><footnote>3</footnote> in condimentum.<footnote>4</footnote></seg>
    </text>
<deposition>

http://xsltfiddle.liberty-development.net/pPgCcon上的XSL 3.0

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml"/>

  <xsl:template match="seg">
    <seg>
      <xsl:for-each select="./attribute() | text() | *">
          <xsl:copy-of select="."/>
      </xsl:for-each>
      <xsl:copy-of select="./following::*[1]"/>
    </seg>
  </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/pPgCcon/1的另一个版本

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="3.0">

<xsl:mode on-no-match="shallow-copy"/>

<xsl:output method="xml"/>

<xsl:template match="seg">
  <seg>
      <xsl:for-each select="./attribute() | text() | *">
          <xsl:copy-of select="."/>
      </xsl:for-each>
      <xsl:copy-of select="./following::node()[name() = footnote]"/>
  </seg>
</xsl:template>

</xsl:stylesheet>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-07 18:49:37

我认为您需要两个模板,一个用于seg复制以下同级footnote,另一个用于防止标识模板复制其输入位置的脚注:

代码语言:javascript
复制
  <xsl:template match="seg">
      <seg>
          <xsl:apply-templates select="@* | node()"/>
          <xsl:copy-of select="following-sibling::node()[1][self::footnote]"/>
      </seg>
  </xsl:template>

  <xsl:template match="deposition/text//footnote[preceding-sibling::node()[1][self::seg]]"/>

http://xsltfiddle.liberty-development.net/pPgCcon/3

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48140256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档