我正在尝试生成一个计数器,并使用xslt中的position()函数成功地生成了一个计数器。
如何在-1001 - 9999范围内创建计数器,使第一个计数器为1001而不是1。
in put xml
<ROOT>
<Row>
<Var>11</Var>
</Row>
<Row>
<Var>11</Var>
</Row>
<Row>
<Var>3</Var>
</Row>
<Row>
<Var>43</Var>
</Row>
<Row>
<Var>51</Var>
</Row>
所需的o/p为
<ROOT>
<Row>
<Var>11</Var>
<seq>1001</seq>
</Row>
<Row>
<Var>11</Var>
<seq>1002</seq>
</Row>
<Row>
<Var>3</Var>
<seq>1003</seq>
</Row>
<Row>
<Var>43</Var>
<seq>1004</seq>
</Row>
<Row>
<Var>51</Var>
<seq>1005</seq>
</Row>
..
谢谢,维基
发布于 2018-02-15 20:53:38
从字面上讲,XPath 2和更高版本允许您编写1000 to 9999来创建这些数字的序列。我不确定这是否适合你的问题,因为你没有提供任何上下文,比如你拥有的输入和你想要的输出。在XSLT3(而不是2)中,通常用于对事物进行计数或编号的xsl:number元素也有一个start-at属性,请参见https://www.w3.org/TR/xslt-30/#element-number。
由于您似乎希望对输入中的元素进行编号,因此我认为正确的方法是使用xsl:number,因此对于XSLT3,您可以简单地这样做:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output indent="yes"/>
<xsl:template match="Row">
<xsl:copy>
<xsl:apply-templates/>
<seq>
<xsl:number start-at="1001"/>
</seq>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>http://xsltfiddle.liberty-development.net/jyyiVhq
使用XSLT2,您当然可以在一个变量中使用xsl:number,然后在1000,例如
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"
exclude-result-prefixes="xs">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:output indent="yes"/>
<xsl:template match="Row">
<xsl:copy>
<xsl:apply-templates/>
<seq>
<xsl:variable name="count" as="xs:integer"><xsl:number/></xsl:variable>
<xsl:value-of select="$count + 1000"/>
</seq>
</xsl:copy>
</xsl:template>
</xsl:transform>http://xsltransform.hikmatu.com/jyyiVhn
我不认为在你的用例中使用to表达式是有意义的,如果你想创建一些新的元素,比如
<xsl:template match="body">
<xsl:copy>
<xsl:for-each select="1001 to 1100">
<seq>
<xsl:value-of select="."/>
</seq>
</xsl:for-each>
</xsl:copy>
</xsl:template>发布于 2018-02-16 15:01:53
我实现了如下请求:
<xsl:template match="/">
<ROOT>
<xsl:apply-templates />
</ROOT>
</xsl:template>
<xsl:template match="ROOT/Row">
<Row>
<Var>
<xsl:value-of select="Var"/>
</Var>
<seq>
<xsl:value-of select="1000 + position()" />
</seq>
</Row>
</xsl:template>发布于 2018-02-22 02:52:18
请注意,使用XSLT 3.0,您可以
<seq>
<xsl:number start-at="1001" />
</seq> https://stackoverflow.com/questions/48807082
复制相似问题