如果满足条件,则XLS创建新元素

时间:2019-11-10 17:15:44

标签: xslt xslt-2.0

我收到此错误:

  

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

如果满足特定条件,当我试图关闭并打开新的fo:block时。

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

该怎么写?

我正在尝试做的完整示例:

<fo:block>
<xsl:for-each select="/article/front/article-meta/contrib-group/contrib[@contrib-type='author']">
    <fo:basic-link show-destination="new" external-destination="url({$link})" >
        <fo:inline>&#160;<xsl:value-of select="name/given-names" />&#160;<xsl:value-of select="name/surname" />&#160;<fo:inline font-size="8pt" vertical-align="super" font-family="HelveticaNeueLTCom-Lt_1" padding-right="8pt" padding-left="-8pt"><xsl:for-each select="xref[@ref-type='aff']"><xsl:value-of select="sup" /><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each></fo:inline></fo:inline>
    </fo:basic-link>
    <xsl:if test=".[@pdf_break='true']">
        </fo:block><fo:block>
    </xsl:if>
</xsl:for-each>

所以基本上应该是:

<fo:block>
Some amount of authors listed with links
</fo:block>

当pdf_break从未为true时,然后:

<fo:block>
Some amount of authors listed with links
</fo:block>
<fo:block>
More authors listed with links
</fo:block>

当属性为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>

2 个答案:

答案 0 :(得分:1)

我认为,如果您有权使用XSLT 2或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>

https://xsltfiddle.liberty-development.net/3NSSEv4/1是一个最小的示例,当然,您需要为XSL-FO转换中剩余的XML输入添加模板。

答案 1 :(得分:1)

使用:

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

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

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

XSLT 1.0解决方案:

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