在 XML 中显示值之前,我可以检查“value-of”值吗?

huangapple go评论83阅读模式
英文:

Is there a way for me to check 'value-of' value before displaying value in xml?

问题

Hi, I'm here to provide the translated content:

你好,我是 XML/XSL/XSLT 的初学者。

我有这个代码:xsl:value-of select="/root/parent/child/value[@name='actualValue'],它从数据库中获取值。

我想要做的是,在将其显示在 DOM 上之前,检查 actualValue 是否包含特定字符。

如果(包含(actualValue,'abc'))
那么(

actualValue

)

逻辑大致如上,如果有道理的话?

我在一些关于 xsl 的讨论中看到了xsl:if,所以我尝试了:

<xsl:if test="contains('actualValue','abc')">

实际值为

答案1

得分: 2

你可能可以这样做:

<xsl:variable name="myVar" select="/root/parent/child/value[@name='actualValue']" />
<xsl:if test="contains($myVar, 'abc')">
    <div>
        <xsl:value-of select="$myVar"/>
    </div>
</xsl:if>

我说“可能”是因为你没有提供一个可复制的示例供我们测试。

而且我们不知道这是否实际上是解决问题的最佳方法;也许更好的方法是使用如下表达式:

/root/parent/child/value[@name='actualValue'][contains(., 'abc')]
英文:

You could probably do something like:

&lt;xsl:variable name=&quot;myVar&quot; select=&quot;/root/parent/child/value[@name=&#39;actualValue&#39;]&quot; /&gt;
&lt;xsl:if test=&quot;contains($myVar, &#39;abc&#39;)&quot;&gt;
	&lt;div&gt;
		&lt;xsl:value-of select=&quot;$myVar&quot;/&gt;
	&lt;/div&gt;
&lt;/xsl:if&gt;

I say "probably" because you did not provide a reproducible example we could use for testing.

And we don't know if that's actually the best way to solve the problem; perhaps a better way would be to use an expression like:

/root/parent/child/value[@name=&#39;actualValue&#39;][contains(., &#39;abc&#39;]

huangapple
  • 本文由 发表于 2023年7月6日 22:07:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定