xml 'Null' 的 XSLT/XPath 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15620469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:56:46  来源:igfitidea点击:

XSLT/XPath test for 'Null'

xmlxsltxpath

提问by MizAkita

I have the following which checks for a string within the xml... however, I need to create another test to check for 'Null' or no text within the variable $validItemsat the 1st line...

我有以下检查xml中的字符串......但是,我需要创建另一个测试来检查$validItems第一行变量中的“空”或无文本......

<xsl:if test="$validItems[(Caption | CalltoAction)[string(.)]]">
 <div class="text">
   <xsl:choose>
     <xsl:when test="$horizontal">
       <div class="holder">
         <div class="frame">
           <div class="slides-descriptions">
             <xsl:apply-templates select="$validItems" mode="horizontal"/>
           </div>
           <div class="switcher"/>
         </div>
       </div>
     </xsl:when>
     <xsl:otherwise>
       <div class="slides-descriptions">
         <xsl:apply-templates select="$validItems" mode="vertical"/>
       </div>
       <div class="switcher"/>
     </xsl:otherwise>
   </xsl:choose>
 </div>
</xsl:if>

How would I go about testing the variable xsl:if test=$validItems?

我将如何测试变量 xsl:if test=$validItems?

回答by LarsH

If I understand what you're asking for,

如果我明白你的要求,

<xsl:if test="$validItems[(Caption | CalltoAction)[not(string(.))]]">

will do it. In other words, "if there is an element in the $validItems node-set that has a Caption or CalltoAction child element whose string value is empty". You could also say

会做的。换句话说,“如果 $validItems 节点集中的元素具有字符串值为空的 Caption 或 CalltoAction 子元素”。你也可以说

<xsl:if test="$validItems[(Caption | CalltoAction)[. = '']]">

回答by Michael Kay

There is no such thing as "Null" in the XPath data model.

XPath 数据模型中没有“Null”这样的东西。

As for "no text within the variable $validItems" it would help to know the type of this variable. If it's a single element node (as might appear from the code sample you have shown), and what you want to test is that it has no text node children, the test would be not($ValidItems/text()). If you want to test that it has no text node descendants, try not(string($validItems))or equivalently, $validItems=''.

至于“变量 $validItems 中没有文本”,了解此变量的类型会有所帮助。如果它是单个元素节点(如您展示的代码示例中所示),并且您要测试的是它没有文本节点子节点,则测试将是not($ValidItems/text()). 如果要测试它是否没有文本节点后代,请尝试not(string($validItems))或等效地,$validItems=''.

回答by Ring

None of these answers worked for me, I don't know if its because I'm using xpath via ODK xforms. I did get it working using this solution:

这些答案都不适合我,我不知道是不是因为我通过 ODK xforms 使用 xpath。我确实使用此解决方案使其正常工作:

string-length(string(/path/to/node)) = 0

I have tested this on dates and numbers.

我已经在日期和数字上对此进行了测试。