这是两个查询但两者都相似(我认为)并且都涉及从元素中计算或减去值。
我需要处理下面的示例输入文件,以便计算标有<!-- must be counted -->
的元素,并减去元素<fpage>
和<lpage>
。然后,计数和减法应该创建一个<counts>
元素,如下面的预期输出文件所示。
问题:需要使用xslt 1.0版完成
示例输入文件:
<article>
<front>
<article-meta>
<fpage>100</fpage> <!-- first page -->
<lpage>117</lpage> <!-- last page -->
</article-meta>
</front>
<body>
<table-wrap></table-wrap> <!-- must be counted -->
<fig></fig> <!-- must be counted -->
<fig></fig> <!-- must be counted -->
<disp-formula></disp-formula> <!-- must be counted -->
</body>
<back>
<ref-list>
<ref></ref> <!-- must be counted -->
<ref></ref> <!-- must be counted -->
<ref></ref> <!-- must be counted -->
<ref></ref> <!-- must be counted -->
<ref></ref> <!-- must be counted -->
</ref-list>
</back>
</article>
预期的输出文件:
<article>
<front>
<article-meta>
<fpage>100</fpage> <!-- first page -->
<lpage>117</lpage> <!-- last page -->
<counts> <!-- this element must be created using XSLT -->
<table-count count="1"/> <!-- should get the total number of <table-wrap> elements -->
<ref-count count="5"/> <!-- should get the total number of <ref> elements -->
<fig-count count="2"/> <!-- should get the total number of <fig> elements -->
<eqs-count count="1"/> <!-- should get the total number of <disp-formula> elements -->
<page-count count="17"/> <!-- should get the value of <lpage> minus <fpage> elements -->
</counts>
</article-meta>
</front>
<body>
<table-wrap></table-wrap>
<fig></fig>
<fig></fig>
<disp-formula></disp-formula>
</body>
<back>
<ref-list>
<ref></ref>
<ref></ref>
<ref></ref>
<ref></ref>
<ref></ref>
</ref-list>
</back>
</article>
知道怎么做吗?
答案 0 :(得分:0)
尝试类似:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<counts>
<table-count count="{count(//table-wrap)}"/> <!-- total number of <table-wrap> elements -->
<ref-count count="{count(//ref)}"/> <!-- total number of <ref> elements -->
<fig-count count="{count(//fig)}"/> <!-- total number of <fig> elements -->
<eqs-count count="{count(//disp-formula)}"/> <!-- total number of <disp-formula> elements -->
<page-count count="{article/front/article-meta/lpage - article/front/article-meta/fpage}"/> <!-- the value of <lpage> minus <fpage> elements -->
</counts>
</xsl:template>
</xsl:stylesheet>
注意:这将从/
根节点开始计算XML文档中任何位置的指定元素。一种更有效的方法是指定计数元素的确切路径(它对页面计数的处理方式)。
编辑:
要在<lpage>
之后立即插入计数,而不对输入进行任何其他更改,请尝试:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="lpage">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<counts>
<table-count count="{count(//table-wrap)}"/> <!-- total number of <table-wrap> elements -->
<ref-count count="{count(//ref)}"/> <!-- total number of <ref> elements -->
<fig-count count="{count(//fig)}"/> <!-- total number of <fig> elements -->
<eqs-count count="{count(//disp-formula)}"/> <!-- total number of <disp-formula> elements -->
<page-count count="{. - ../fpage}"/> <!-- the value of <lpage> minus <fpage> elements -->
</counts>
</xsl:template>
</xsl:stylesheet>