XLS 如果条件满足则创建新元素
XLS Create New Element If Condition Meet
我收到此错误:
The element type "xsl:if" must be terminated by
the matching end-tag ""
当我尝试关闭并打开一个新的 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> <xsl:value-of select="name/given-names" /> <xsl:value-of select="name/surname" /> <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>
我认为,如果您可以访问 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 是一个最小样本,当然您需要为剩余的 XML 输入添加模板以进行 XSL-FO 转换。
使用:
<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>
我收到此错误:
The element type "xsl:if" must be terminated by the matching end-tag ""
当我尝试关闭并打开一个新的 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> <xsl:value-of select="name/given-names" /> <xsl:value-of select="name/surname" /> <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>
我认为,如果您可以访问 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 是一个最小样本,当然您需要为剩余的 XML 输入添加模板以进行 XSL-FO 转换。
使用:
<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>