如何检查 XSLT 中的 XML 值是否为 nil
在 XML 文档中,我有一些地址数据..
<zip>08001</zip>
<zipPlus xsi:nil="true" />
并且
<zip>08002</zip>
<zipPlus>4512</zipPlus>
On 只想在有要使用的值时显示 zip 加值。 (就本示例而言,我不关心它是否是正确的 zip plus 格式)
尝试在 XSLT 中使用以下代码片段似乎永远无法正常工作,我认为这与我检查的方式有关对于 xsl:nil 值
<EmployerZipCode>
<xsl:value-of select="zip"/>
<xsl:if test="zipPlus != @xsl:nil">
<xsl:value-of select="'-'"/>
<xsl:value-of select="zipPlus"/>
</xsl:if>
<xsl:value-of select="$sepChar"/> <!--this is a comma -->
</EmployerZipCode>
我得到的结果始终
08001,
08002,
不是
08001,
08002-4512,
检查 XSLT 中 nil-led 元素的正确方法是什么? 还有其他方法可以解决这个问题并获得我想要的结果吗?
In an XML document I have some address data..
<zip>08001</zip>
<zipPlus xsi:nil="true" />
and
<zip>08002</zip>
<zipPlus>4512</zipPlus>
On only want to bother displaying the zip plus value if there is a value to use. (for the purposes of this example, I don't care if its a correct zip plus format or not)
Trying to use the following snippet in an XSLT never seems to work right, and I think it has to do with how I am checking for the xsl:nil value
<EmployerZipCode>
<xsl:value-of select="zip"/>
<xsl:if test="zipPlus != @xsl:nil">
<xsl:value-of select="'-'"/>
<xsl:value-of select="zipPlus"/>
</xsl:if>
<xsl:value-of select="$sepChar"/> <!--this is a comma -->
</EmployerZipCode>
The results I get are always
08001,
08002,
not
08001,
08002-4512,
What is the proper way to check for nil-led elements in XSLT?
Are there any other ways to get around this problem and get the result I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在XSLT 2.0中,出于我从未完全理解的原因,有一个自定义函数
In XSLT 2.0, for reasons I have never fully understood, there is a custom function
经过更多测试后,没有给出涉及检查 nil 属性的可靠答案。
我不得不求助于使用 string-length() 来获得我需要的结果。
After some more testing, none of answers given that involve checking the nil attribute work reliably.
I had to resort to using string-length() to get the result I needed.
很奇怪你没有让它发挥作用。也许您缺少名称空间声明,或者将
xsi
前缀更改为xsl
是转换中看不见的拼写错误。检查一下比较好。这是我的测试:XSLT 1.0 和 Saxon 6.5
给定输入:
产生:
It's very strange that you didn't get it to work. Perhaps it might be you are missing namespace declaration or the prefix change
xsi
toxsl
is an unseen typo in your transform. Check better. Here my test:XSLT 1.0 with Saxon 6.5
Given the input:
Produces:
它对我有用
源 XML :
XSLT :
结果 XML
It works for me
Source XML :
XSLT :
Result XML