gpt4 book ai didi

xml - 在 xsl 中拆分字符串并更新属性

转载 作者:数据小太阳 更新时间:2023-10-29 02:36:27 25 4
gpt4 key购买 nike

我有一个具有以下结构的 xml 文件

    <project>
<dependency id="abc" version="1.2.3.4"/>
</project>

我需要读取此 xml 并使用 id 和 version 的值更新另一个 xml。早些时候我用如下的 xsl 做了这个,它工作正常:

    <xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>

<xsl:attribute name="version">
<xsl:value-of select="@version"/>
</xsl:attribute>

现在我需要设置version的属性值为[1.2,1.3),我该怎么做呢?我在下面尝试过类似的方法,但我不认为我无处可去。

    <xsl:variable name="MinVersion"/>
<xsl:variable name="MaxVersion"/>

<xsl:for-each select="tokenize(@version,'.')">

<xsl:if test="(position( )) = 2">
<xsl:value-of select="concat($MinVersion,.)"/>
</xsl:if>
<xsl:otherwise>
<xsl:value-of select="concat($MinVersion,.,'.')"/>
</xsl:otherwise>

<xsl:if test="(position( )) = 2">
<xsl:value-of select="concat($MaxVersion,number(.)+1)"/>
</xsl:if>
<xsl:otherwise>
<xsl:value-of select="concat($MaxVersion,.,'.')"/>
</xsl:otherwise>
</xsl:for-each>

我该怎么做?

更新:我尝试在 Visual Studio 中对此进行调试,但收到错误消息,指出 tokenize 不是可识别的函数。经过一些搜索发现.NET 框架仅支持 XSLT 1.0 处理器。任何 1.0 的解决方案都会有所帮助。

最佳答案

试试下面的,希望它能解决你的问题。(我已经改变了我以前的答案,检查下面的新答案。)

<xsl:variable name="testVersion" select="@version"></xsl:variable>
<xsl:variable name="separator" select="'.'"></xsl:variable>


<xsl:if test="string-length($testVersion) > 0">

<xsl:variable name="firstnumber" select="substring-before($testVersion, $separator)"/>
<xsl:variable name="after-separator" select="substring-after($testVersion, $separator)"/>
<xsl:variable name="secondnumber" select="substring-before($after-separator, $separator)"/>

<xsl:variable name="outputvalue">
<xsl:text>[</xsl:text>
<xsl:value-of select="$firstnumber"></xsl:value-of>
<xsl:text>.</xsl:text>
<xsl:value-of select="$secondnumber"></xsl:value-of>
<xsl:text>,</xsl:text>
<xsl:value-of select="$firstnumber"></xsl:value-of>
<xsl:text>.</xsl:text>
<xsl:value-of select="number($secondnumber)+1"></xsl:value-of>
<xsl:text>)</xsl:text>
</xsl:variable>

<xsl:value-of select="$outputvalue"/>
</xsl:if>

关于xml - 在 xsl 中拆分字符串并更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13013295/

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