【问题标题】:XSL test two conditionsXSL 测试两个条件
【发布时间】:2023-02-13 13:15:31
【问题描述】:

我有以下 XML。我想检查具有名为“Key”的元素且值为“response”的 FormField 节点是否为名为“Value”的元素设置了值。如果值元素为空则返回 false 否则返回 true:

<Form>
    <FormField>
        <Value>Jumper</Value>
        <Key>item</Key>
        <Label>Item</Label>
        <Type>string</Type>
    </FormField>
    <FormField>
        <Value>unsuccessful</Value>
        <Key>response</Key>
        <Label>Response</Label>
        <Type>string</Type>
    </FormField>
    <FormField>
        <Value/>
        <Key>notes</Key>
        <Label>Notes</Label>
        <Type>string</Type>
    </FormField>
<Form>

因此,例如以下将返回 true

<FormField>
        <Value>unsuccessful</Value>
        <Key>response</Key>
        <Label>Response</Label>
        <Type>string</Type>
    </FormField>

以下内容将返回 false

<FormField>
        <Value/>
        <Key>response</Key>
        <Label>Response</Label>
        <Type>string</Type>
    </FormField>

我怎样才能做到这一点?

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    如果“有一个值”是指“包含一个文本节点”,那么请尝试:

    <xsl:value-of select="exists(/Form/FormField[Key='response']/Value/text())"/>
    

    或者:

    <xsl:value-of select="boolean(/Form/FormField[Key='response']/Value/text())"/>
    

    【讨论】:

    • 不按值我的意思是如果具有值为“response”的键的表单字段在值字段中也有一个值 - 所以在上面的示例中它具有“不成功”的值所以条件应该返回真
    • 我已经更新了问题以使其更清楚。
    • 是的,但术语“在值字段中具有值”是模棱两可的。这就是为什么我限定我的答案。如果 Value 元素中的 Value 元素具有子元素 Key 其字符串值为“response”,则上述表达式将返回“true”(如您的示例中所示)。如果删除“不成功”文本并将元素留空,则结果将为“false”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多