[英]XSL function that return square number list with an parameter
I want a function that take an integer parameter n and returns a list of the squares of integers between 1 and n. 我想要一个带有整数参数n并返回1到n之间的整数平方的列表的函数。 If n < 1 then the function returns the empty sequence.
如果n <1,则该函数返回空序列。 For this I use the manufacturer XSL sequence.
为此,我使用制造商的XSL序列。
<xsl:function name="squareNumberList" as="xsd:string">
<!-- use a param that delimite the list -->
<xsl:param name="value" as="xsd:decimal *"/>
<!-- create a sequence of the size of $value and containt 1-2-3-...-value -->
<xsl:sequence name="seq" select="1 to $value"/>
<!-- if the value if lesser than 1 return empty sequence -->
<xsl:if test="value < 1">
<xsl:value-of select="$value"></xsl:value>
</xsl:if>
<!-- calculate 1*1, 2*2, 3*3 ... value*value and store result in res -->
<xsl:variable name="res" as="xsd:decimal *">
<xsl:for-each select="$seq">
<xsl:sequence select=". * ."/>
</xsl:for-each>
</xsl:variable>
<!-- return the result -->
<xsl:value-of select="$res"></xsl:value-of>
</xsl:function>
Can you confirm that this function is well defined ? 您可以确认此功能定义正确吗?
Aren't you making this more complicated than necessary? 您不是把这个复杂化了吗? A simple one-liner:
一个简单的单线:
<xsl:sequence select="for $i in 1 to $value return $i * $i" />
should do the job. 应该做的工作。
An XPath 2.0 suggestion was already posted, as XPath 3.0 is supported in Saxon 9.7 (all editions) here is an alternative using !
XPath 2.0建议已经发布,因为Saxon 9.7(所有版本)都支持XPath 3.0,这里是使用
!
的替代方法!
: :
<xsl:function name="mf:square-list1" as="xs:integer*">
<xsl:param name="value" as="xs:integer"/>
<xsl:sequence select="(1 to $value) ! (. * .)"/>
</xsl:function>
Using higher order functions and for-each
with commercial editions of Saxon 9.6/7 we can also use 使用更高阶的函数并
for-each
Saxon 9.6 / 7的商业版本进行逐行处理,我们还可以使用
<xsl:function name="mf:square-list2" as="xs:integer*">
<xsl:param name="value" as="xs:integer"/>
<xsl:sequence select="for-each(1 to $value, function($n) { $n * $n })"/>
</xsl:function>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.