移动时复制元素'关注'同一元素内的元素

时间:2018-01-07 18:28:11

标签: xslt xpath

在下面这个基于文本的XML示例中,使用XSL 3.0 / Saxon,我想将紧跟在new Vue({ el:'#app', data: { message: 'testing', defaults:{ selectBox : ['VUE','REACT','ANGULAR']},//some default vaues items: [{val:'VUE'},{val: 'REACT'}] //intial two items }, methods:{ addNewItem:function(){ //add new dynamic items this.items.push({val:''}); } } }); 元素之后的任何footnote移动到 seg元素,只有 结束seg之前 之后的任何文字()和儿童可能在</seg>内。< / p>

我在http://xsltfiddle.liberty-development.net/pPgCconhttp://xsltfiddle.liberty-development.net/pPgCcon/1的实验   (a)不优雅,而且   (b)未能捕获权利seg元素

源XML:

following::

这是目标XML - 请注意<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> 之后<footnote>4</footnote>之外的segseg之外移动到text()元素的方式。

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

<?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的另一个版本:

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

1 个答案:

答案 0 :(得分:2)

我认为你需要两个模板,一个用于seg复制以下兄弟footnote,另一个用于防止身份模板在其输入位置复制脚注:

  <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