gpt4 book ai didi

xslt - 在 XSLT 中随机选择一个节点

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:49 26 4
gpt4 key购买 nike

我对 XSLT 中的某种随机函数有疑问。

我有一个非常简化的 XML 文件,看起来与此类似:

<node id="1198">
<node id="1201">
<data alias="name">Flemming</data>
<data alias="picture">1200</data>
</node>
<node id="1207">
<data alias="name">John</data>
<data alias="picture">1205</data>
</node>
<node id="1208">
<data alias="name">Michael</data>
<data alias="picture">1206</data>
</node>
</node>

我想要一些 XSLT,随机获取节点 ID 之一并将其放入名为“choosenNode”的变量中。像这样,如果ID为1207的节点是选中的节点:

<xsl:variable name="choosenNode" value="1207" />

我该怎么做? XSLT 中有随机函数吗?顺便说一下,我希望在包含 XSLT 的每个页面上刷新该变量。

我在 Umbraco CMS 工作,如果这对你们有帮助的话。

谢谢,-金

最佳答案

在 Umbraco 中你可以做这样的事情:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath">

<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<!-- This should probably be a macro parameter so you can use this elsewhere-->
<xsl:variable name="parentNode" select="1048"/>

<xsl:template match="/">

<xsl:variable name="numberOfNodes" select="count(umbraco.library:GetXmlNodeById($parentNode)/node)"/>

<xsl:variable name="randomPosition" select="floor(Exslt.ExsltMath:random() * $numberOfNodes) + 1"/>

<xsl:variable name="randomNode" select="umbraco.library:GetXmlNodeById($parentNode)/node [position() = $randomPosition]"/>

<!--
You now have the node in the $randomNode variable
If you just want the id then you can do an XPath query on the variable
or you can modify the XPath above to get the property you are after rather than
the whole node
-->

<xsl:value-of select="$randomNode/@nodeName" />

</xsl:template>
</xsl:stylesheet>

希望这对您有所帮助。

蒂姆

关于xslt - 在 XSLT 中随机选择一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1389127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com