如何检查 XSLT 中的 XML 值是否为 nil

发布于 2024-11-14 14:11:06 字数 892 浏览 10 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花想c 2024-11-21 14:11:06
<xsl:if test="not(zipPlus/@xsi:nil='true')">
<xsl:if test="not(zipPlus/@xsi:nil='true')">
苯莒 2024-11-21 14:11:06

在XSLT 2.0中,出于我从未完全理解的原因,有一个自定义函数

test="not(nilled(zipPlus))"

In XSLT 2.0, for reasons I have never fully understood, there is a custom function

test="not(nilled(zipPlus))"
三生一梦 2024-11-21 14:11:06

经过更多测试后,没有给出涉及检查 nil 属性的可靠答案。

我不得不求助于使用 string-length() 来获得我需要的结果。

<EmployerZipCode>
  <xsl:value-of select="zip"/>
  <xsl:if test="string-length(zipPlus) > 0">
    <xsl:value-of select="'-'"/>
    <xsl:value-of select="zipPlus"/>
  </xsl:if>
  <xsl:value-of select="$sepChar"/>
</EmployerZipCode>

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.

<EmployerZipCode>
  <xsl:value-of select="zip"/>
  <xsl:if test="string-length(zipPlus) > 0">
    <xsl:value-of select="'-'"/>
    <xsl:value-of select="zipPlus"/>
  </xsl:if>
  <xsl:value-of select="$sepChar"/>
</EmployerZipCode>
方觉久 2024-11-21 14:11:06

很奇怪你没有让它发挥作用。也许您缺少名称空间声明,或者将 xsi 前缀更改为 xsl 是转换中看不见的拼写错误。检查一下比较好。这是我的测试:

XSLT 1.0Saxon 6.5

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="test">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="EmployerZipCode">
        <EmployerZipCode>
            <xsl:value-of select="zip"/>
            <xsl:if test="not(zipPlus/@xsi:nil)">
                <xsl:value-of select="'-'"/>
                <xsl:value-of select="zipPlus"/>
            </xsl:if>
            <xsl:value-of select="','"/> <!--this is a comma -->
        </EmployerZipCode>
    </xsl:template>
</xsl:stylesheet>

给定输入:

<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xsi="test">
    <EmployerZipCode>
        <zip>08001</zip>
        <zipPlus xsi:nil="true" />
    </EmployerZipCode>
    <EmployerZipCode>
        <zip>08002</zip>
        <zipPlus>4512</zipPlus>
    </EmployerZipCode>
</test>

产生:

08001,
08002-4512,

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 to xsl is an unseen typo in your transform. Check better. Here my test:

XSLT 1.0 with Saxon 6.5

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="test">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="EmployerZipCode">
        <EmployerZipCode>
            <xsl:value-of select="zip"/>
            <xsl:if test="not(zipPlus/@xsi:nil)">
                <xsl:value-of select="'-'"/>
                <xsl:value-of select="zipPlus"/>
            </xsl:if>
            <xsl:value-of select="','"/> <!--this is a comma -->
        </EmployerZipCode>
    </xsl:template>
</xsl:stylesheet>

Given the input:

<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xsi="test">
    <EmployerZipCode>
        <zip>08001</zip>
        <zipPlus xsi:nil="true" />
    </EmployerZipCode>
    <EmployerZipCode>
        <zip>08002</zip>
        <zipPlus>4512</zipPlus>
    </EmployerZipCode>
</test>

Produces:

08001,
08002-4512,
拥抱影子 2024-11-21 14:11:06

它对我有用

源 XML :

            <zipPlus xsi:nil="true"/>
                      or
            <zipPlus>123456</zipPlus>

XSLT :

            <xsl:if test="not(zipPlus/@xsl:nil='true')">
                <xsl:value-of select="zipPlus"/>
            </xsl:if>

结果 XML

            <zipPlus/>
            or
            <zipPlus>123456</zipPlus>

It works for me

Source XML :

            <zipPlus xsi:nil="true"/>
                      or
            <zipPlus>123456</zipPlus>

XSLT :

            <xsl:if test="not(zipPlus/@xsl:nil='true')">
                <xsl:value-of select="zipPlus"/>
            </xsl:if>

Result XML

            <zipPlus/>
            or
            <zipPlus>123456</zipPlus>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文