我想测试一下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:
<OSM>
<EstablishmentDetail>
<RatingValue>5</RatingValue>
<RatingDate>2008-05-15</RatingDate>
</EstablishmentDetail>
<EstablishmentDetail>
<RatingValue>AwaitingInspection</RatingValue>
<RatingDate xsi:nil="true"/>
</EstablishmentDetail>
</OSM>XSL的一个片段:
<xsl:template>
"Value": "<xsl:value-of select="if (nilled(RatingDate)) then RatingValue else 'XX' "/>",
</xsl:template>它正在产生输出,但两者都是'XX‘。这只是语法错误吗?
发布于 2022-10-18 11:59:17
需要在输入文档中添加命名空间:
<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()函数中获得正确的值,因此替换了一个用户函数:
<?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>它产生:
<?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。
发布于 2022-10-18 12:03:43
函数https://www.w3.org/TR/xpath-functions/#func-nilled用于使用支持模式的XSLT和经过验证的输入,也就是说,如果您使用Saxon,您可以期望它完成它的工作:
实际上,该函数只对属性为
“xsi:nil=”的元素节点返回true,并根据定义元素为nillable的架构成功验证;
。
https://stackoverflow.com/questions/74109716
复制相似问题