首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试xsi:nil=“真”

测试xsi:nil=“真”
EN

Stack Overflow用户
提问于 2022-10-18 10:49:14
回答 2查看 40关注 0票数 1

我想测试一下xsi:nil=的“真”。

我在这些帖子中分别使用了kjhughes和Michael Kay的回复。

How to implement if-else statement in XSLT?

How do I check if XML value is nil in XSLT

XML:

代码语言:javascript
复制
<OSM>
    <EstablishmentDetail>
         <RatingValue>5</RatingValue>
         <RatingDate>2008-05-15</RatingDate>
    </EstablishmentDetail>
    <EstablishmentDetail>
         <RatingValue>AwaitingInspection</RatingValue>
         <RatingDate xsi:nil="true"/>
    </EstablishmentDetail>
</OSM>

XSL的一个片段:

代码语言:javascript
复制
<xsl:template>
"Value": "<xsl:value-of select="if (nilled(RatingDate)) then RatingValue else 'XX' "/>",
</xsl:template>

它正在产生输出,但两者都是'XX‘。这只是语法错误吗?

EN

回答 2

Stack Overflow用户

发布于 2022-10-18 11:59:17

需要在输入文档中添加命名空间:

代码语言:javascript
复制
<OSM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <EstablishmentDetail>
         <RatingValue>5</RatingValue>
         <RatingDate>2008-05-15</RatingDate>
    </EstablishmentDetail>
    <EstablishmentDetail>
         <RatingValue>AwaitingInspection</RatingValue>
         <RatingDate xsi:nil="true"/>
    </EstablishmentDetail>
</OSM>

我没有从标准的fn:nilled()函数中获得正确的值,因此替换了一个用户函数:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:l="local:functions">
  
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <RatingValues>
      <xsl:for-each select="//EstablishmentDetail">
        <EstablishmentDetail index="{position()}" >
          <RatingValue>
            <xsl:value-of select="('XX'[l:nilled(current()/RatingDate) ], 
                                    current()/RatingValue)[1]" />
          </RatingValue>
          <Nilled>
            <xsl:value-of select="nilled(RatingDate)" />
          </Nilled>          
        </EstablishmentDetail>
      </xsl:for-each>
    </RatingValues>
  </xsl:template>
  
  <xsl:function name="l:nilled" as="xs:boolean" >
    <xsl:param name="e" as="element()" />
    
    <xsl:sequence select="exists($e/@xsi:nil) and $e/@xsi:nil eq 'true'" />
  </xsl:function>
  
</xsl:stylesheet>

它产生:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<RatingValues xmlns:l="local:functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <EstablishmentDetail index="1">
      <RatingValue>5</RatingValue>
      <Nilled>false</Nilled>
   </EstablishmentDetail>
   <EstablishmentDetail index="2">
      <RatingValue>XX</RatingValue>
      <Nilled>false</Nilled>
   </EstablishmentDetail>
</RatingValues>

输出中的Nilled元素只是为了说明我在这两种情况下都得到了false

票数 0
EN

Stack Overflow用户

发布于 2022-10-18 12:03:43

函数https://www.w3.org/TR/xpath-functions/#func-nilled用于使用支持模式的XSLT和经过验证的输入,也就是说,如果您使用Saxon,您可以期望它完成它的工作:

实际上,该函数只对属性为

“xsi:nil=”的元素节点返回true,并根据定义元素为nillable的架构成功验证;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74109716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档