Xslt XLS在条件满足时创建新元素

Xslt XLS在条件满足时创建新元素,xslt,xslt-2.0,Xslt,Xslt 2.0,我收到这个错误: 元素类型“xsl:if”必须由 匹配的结束标记“” 当我试图关闭和打开一个新的fo:block时,如果满足某个条件 <xsl:if test=".[@pdf_break='true']"> </fo:block><fo:block> </xsl:if> 当属性为true时 XML示例: <contrib-group> <contrib equal-contrib="yes" contri

我收到这个错误:

元素类型“xsl:if”必须由 匹配的结束标记“”

当我试图关闭和打开一个新的
fo:block
时,如果满足某个条件

<xsl:if test=".[@pdf_break='true']">
        </fo:block><fo:block>
</xsl:if>
当属性为
true

XML示例:

<contrib-group>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surnameExample1</surname>
            <given-names>Example1</given-names>
        </name>
        <xref ref-type='aff' rid='ID1'><sup>1</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example2</surname>
            <given-names>Example2</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
        <xref ref-type='aff' rid='ID3'><sup>3</sup></xref>
        <xref ref-type='aff' rid='ID4'><sup>4</sup></xref>
        <xref ref-type='aff' rid='ID5'><sup>5</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='true'>
        <name>
            <surname>Example3</surname>
            <given-names>Example3</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example4</surname>
            <given-names>Example4</given-names>
        </name>
        <xref ref-type='aff' rid='ID6'><sup>6</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example5</surname>
            <given-names>Example15</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
</contrib-group>


我认为,如果您有权访问XSLT2或3,您可以使用

  <xsl:template match="contrib-group">
      <xsl:for-each-group select="contrib[@contrib-type = 'author']" group-starting-with="*[@pdf_break = 'true']">
          <fo:block>
              <xsl:apply-templates select="current-group()"/>
          </fo:block>
      </xsl:for-each-group>
  </xsl:template>

是一个最小的示例,当然您需要为XSL-FO转换中的剩余XML输入添加模板。

使用:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/*">
   <doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:for-each-group select="contrib" group-ending-with="contrib[@pdf_break = 'true']">
      <fo:block>
         <xsl:for-each select="current-group()">
           <author><xsl:sequence select="name/surname, name/given-names"/></author>
         </xsl:for-each>
      </fo:block>
    </xsl:for-each-group>
    </doc>
  </xsl:template>
</xsl:stylesheet>
<contrib-group>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example1</surname>
            <given-names>Example1</given-names>
        </name>
        <xref ref-type='aff' rid='ID1'><sup>1</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example2</surname>
            <given-names>Example2</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
        <xref ref-type='aff' rid='ID3'><sup>3</sup></xref>
        <xref ref-type='aff' rid='ID4'><sup>4</sup></xref>
        <xref ref-type='aff' rid='ID5'><sup>5</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='true'>
        <name>
            <surname>Example3</surname>
            <given-names>Example3</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example4</surname>
            <given-names>Example4</given-names>
        </name>
        <xref ref-type='aff' rid='ID6'><sup>6</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example5</surname>
            <given-names>Example15</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
</contrib-group>
<doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:block>
      <author>
         <surname>Example1</surname>
         <given-names>Example1</given-names>
      </author>
      <author>
         <surname>Example2</surname>
         <given-names>Example2</given-names>
      </author>
      <author>
         <surname>Example3</surname>
         <given-names>Example3</given-names>
      </author>
   </fo:block>
   <fo:block>
      <author>
         <surname>Example4</surname>
         <given-names>Example4</given-names>
      </author>
      <author>
         <surname>Example5</surname>
         <given-names>Example15</given-names>
      </author>
   </fo:block>
</doc>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kInGroup" match="contrib" 
          use="generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])"/>

  <xsl:template match=
     "contrib[generate-id()
             = generate-id(key('kInGroup',
                                generate-id(preceding-sibling::contrib[@pdf_break='true']
                                                                                 [1]
                              )
                           )[1]
                         )
             ]">
    <fo:block>
      <xsl:apply-templates mode="inGroup" select=
      "key('kInGroup',
            generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])
           )"/>
    </fo:block>
  </xsl:template>

  <xsl:template match="contrib" mode="inGroup">
     <author><xsl:copy-of select="name/*"/></author>
  </xsl:template>

  <xsl:template match="text()"/>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/*">
   <doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:for-each-group select="contrib" group-ending-with="contrib[@pdf_break = 'true']">
      <fo:block>
         <xsl:for-each select="current-group()">
           <author><xsl:sequence select="name/surname, name/given-names"/></author>
         </xsl:for-each>
      </fo:block>
    </xsl:for-each-group>
    </doc>
  </xsl:template>
</xsl:stylesheet>
<contrib-group>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example1</surname>
            <given-names>Example1</given-names>
        </name>
        <xref ref-type='aff' rid='ID1'><sup>1</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example2</surname>
            <given-names>Example2</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
        <xref ref-type='aff' rid='ID3'><sup>3</sup></xref>
        <xref ref-type='aff' rid='ID4'><sup>4</sup></xref>
        <xref ref-type='aff' rid='ID5'><sup>5</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='true'>
        <name>
            <surname>Example3</surname>
            <given-names>Example3</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example4</surname>
            <given-names>Example4</given-names>
        </name>
        <xref ref-type='aff' rid='ID6'><sup>6</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example5</surname>
            <given-names>Example15</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
</contrib-group>
<doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:block>
      <author>
         <surname>Example1</surname>
         <given-names>Example1</given-names>
      </author>
      <author>
         <surname>Example2</surname>
         <given-names>Example2</given-names>
      </author>
      <author>
         <surname>Example3</surname>
         <given-names>Example3</given-names>
      </author>
   </fo:block>
   <fo:block>
      <author>
         <surname>Example4</surname>
         <given-names>Example4</given-names>
      </author>
      <author>
         <surname>Example5</surname>
         <given-names>Example15</given-names>
      </author>
   </fo:block>
</doc>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kInGroup" match="contrib" 
          use="generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])"/>

  <xsl:template match=
     "contrib[generate-id()
             = generate-id(key('kInGroup',
                                generate-id(preceding-sibling::contrib[@pdf_break='true']
                                                                                 [1]
                              )
                           )[1]
                         )
             ]">
    <fo:block>
      <xsl:apply-templates mode="inGroup" select=
      "key('kInGroup',
            generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])
           )"/>
    </fo:block>
  </xsl:template>

  <xsl:template match="contrib" mode="inGroup">
     <author><xsl:copy-of select="name/*"/></author>
  </xsl:template>

  <xsl:template match="text()"/>
</xsl:stylesheet>

例1
例1
1.
例2
例2
2.
3.
4.
5.
例3
例3
2.
例4
例4
6.
例5
例15
2.
生成所需的结构化输出:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/*">
   <doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:for-each-group select="contrib" group-ending-with="contrib[@pdf_break = 'true']">
      <fo:block>
         <xsl:for-each select="current-group()">
           <author><xsl:sequence select="name/surname, name/given-names"/></author>
         </xsl:for-each>
      </fo:block>
    </xsl:for-each-group>
    </doc>
  </xsl:template>
</xsl:stylesheet>
<contrib-group>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example1</surname>
            <given-names>Example1</given-names>
        </name>
        <xref ref-type='aff' rid='ID1'><sup>1</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example2</surname>
            <given-names>Example2</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
        <xref ref-type='aff' rid='ID3'><sup>3</sup></xref>
        <xref ref-type='aff' rid='ID4'><sup>4</sup></xref>
        <xref ref-type='aff' rid='ID5'><sup>5</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='true'>
        <name>
            <surname>Example3</surname>
            <given-names>Example3</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example4</surname>
            <given-names>Example4</given-names>
        </name>
        <xref ref-type='aff' rid='ID6'><sup>6</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example5</surname>
            <given-names>Example15</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
</contrib-group>
<doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:block>
      <author>
         <surname>Example1</surname>
         <given-names>Example1</given-names>
      </author>
      <author>
         <surname>Example2</surname>
         <given-names>Example2</given-names>
      </author>
      <author>
         <surname>Example3</surname>
         <given-names>Example3</given-names>
      </author>
   </fo:block>
   <fo:block>
      <author>
         <surname>Example4</surname>
         <given-names>Example4</given-names>
      </author>
      <author>
         <surname>Example5</surname>
         <given-names>Example15</given-names>
      </author>
   </fo:block>
</doc>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kInGroup" match="contrib" 
          use="generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])"/>

  <xsl:template match=
     "contrib[generate-id()
             = generate-id(key('kInGroup',
                                generate-id(preceding-sibling::contrib[@pdf_break='true']
                                                                                 [1]
                              )
                           )[1]
                         )
             ]">
    <fo:block>
      <xsl:apply-templates mode="inGroup" select=
      "key('kInGroup',
            generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])
           )"/>
    </fo:block>
  </xsl:template>

  <xsl:template match="contrib" mode="inGroup">
     <author><xsl:copy-of select="name/*"/></author>
  </xsl:template>

  <xsl:template match="text()"/>
</xsl:stylesheet>

例1
例1
例2
例2
例3
例3
例4
例4
例5
例15

XSLT1.0解决方案:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/*">
   <doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:for-each-group select="contrib" group-ending-with="contrib[@pdf_break = 'true']">
      <fo:block>
         <xsl:for-each select="current-group()">
           <author><xsl:sequence select="name/surname, name/given-names"/></author>
         </xsl:for-each>
      </fo:block>
    </xsl:for-each-group>
    </doc>
  </xsl:template>
</xsl:stylesheet>
<contrib-group>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example1</surname>
            <given-names>Example1</given-names>
        </name>
        <xref ref-type='aff' rid='ID1'><sup>1</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example2</surname>
            <given-names>Example2</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
        <xref ref-type='aff' rid='ID3'><sup>3</sup></xref>
        <xref ref-type='aff' rid='ID4'><sup>4</sup></xref>
        <xref ref-type='aff' rid='ID5'><sup>5</sup></xref>
    </contrib>
    <contrib equal-contrib="yes" contrib-type='author' pdf_break='true'>
        <name>
            <surname>Example3</surname>
            <given-names>Example3</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example4</surname>
            <given-names>Example4</given-names>
        </name>
        <xref ref-type='aff' rid='ID6'><sup>6</sup></xref>
    </contrib>
    <contrib contrib-type='author' pdf_break='false'>
        <name>
            <surname>Example5</surname>
            <given-names>Example15</given-names>
        </name>
        <xref ref-type='aff' rid='ID2'><sup>2</sup></xref>
    </contrib>
</contrib-group>
<doc xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:block>
      <author>
         <surname>Example1</surname>
         <given-names>Example1</given-names>
      </author>
      <author>
         <surname>Example2</surname>
         <given-names>Example2</given-names>
      </author>
      <author>
         <surname>Example3</surname>
         <given-names>Example3</given-names>
      </author>
   </fo:block>
   <fo:block>
      <author>
         <surname>Example4</surname>
         <given-names>Example4</given-names>
      </author>
      <author>
         <surname>Example5</surname>
         <given-names>Example15</given-names>
      </author>
   </fo:block>
</doc>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kInGroup" match="contrib" 
          use="generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])"/>

  <xsl:template match=
     "contrib[generate-id()
             = generate-id(key('kInGroup',
                                generate-id(preceding-sibling::contrib[@pdf_break='true']
                                                                                 [1]
                              )
                           )[1]
                         )
             ]">
    <fo:block>
      <xsl:apply-templates mode="inGroup" select=
      "key('kInGroup',
            generate-id(preceding-sibling::contrib[@pdf_break = 'true'][1])
           )"/>
    </fo:block>
  </xsl:template>

  <xsl:template match="contrib" mode="inGroup">
     <author><xsl:copy-of select="name/*"/></author>
  </xsl:template>

  <xsl:template match="text()"/>
</xsl:stylesheet>

当应用于同一XML文档(如上)时,再次生成正确的结构化输出

<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <author>
      <surname>Example1</surname>
      <given-names>Example1</given-names>
   </author>
   <author>
      <surname>Example2</surname>
      <given-names>Example2</given-names>
   </author>
   <author>
      <surname>Example3</surname>
      <given-names>Example3</given-names>
   </author>
</fo:block>
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <author>
      <surname>Example4</surname>
      <given-names>Example4</given-names>
   </author>
   <author>
      <surname>Example5</surname>
      <given-names>Example15</given-names>
   </author>
</fo:block>

例1
例1
例2
例2
例3
例3
例4
例4
例5
例15

是向后的:它必须是
。这是不可能的。XSLT样式表还必须是格式良好的XML文档。一般实体格式良好的结果是XML文档中的逻辑和物理结构被正确嵌套;任何开始标记、结束标记、空元素标记、元素、注释、处理指令、字符引用或实体引用都不能在一个实体中开始,在另一个实体中结束请显示输入的示例-请参阅:。我在示例中添加了
contrib组
级别的输入,如果需要更多的父级,请告诉我。谢谢。我想您应该为每个组使用
组相邻=“@pdf\u break”
组以=“*[@pdf\u break='true']”开头。请参阅中的示例或关于分组或编辑您的问题的任何其他介绍,并在问题末尾向我们展示您希望输入样本的结果。Martin,按照我对问题的理解,似乎必须使用以
结尾的
组,而不是以
@user3783243:开头的
组,这取决于条件。你可以通过Gmail的dnovatchev与我联系