xml 根据 xslt 中的条件将字符串分配给变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11322671/
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:33:29  来源:igfitidea点击:

Assigning a string to a variable depending on condition in xslt

xmlxsltxpathxslt-1.0xslt-2.0

提问by harsh

I want to assign a value to a variable if a particular attribute returns a particular value. In here I want to assign the value "young" to vaiable "person" if pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename) is "cengie". Is that possible?

如果特定属性返回特定值,我想为变量赋值。在这里,如果 pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename) 是“cengie ”。那可能吗?

<xsl:variable
  name='person' select='pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename)'>
</xsl:variable>

回答by Tim C

You can put any xslt code within an xsl:variableand the result will be assigned to the variable. In this case you could make use of an xsl:ifto check your condition

您可以将任何 xslt 代码放在xsl:variable 中,结果将分配给该变量。在这种情况下,您可以使用xsl:if来检查您的情况

<xsl:variable name="person"> 
    <xsl:if test="pr:all[@pr:name=current()/@cx:name]/pr:properties[@ls:middlename='cengie']">
       <xsl:text>young</xsl:text>
    </xsl:if>
</xsl:variable> 

If you wanted an 'else' case here, you would use xsl:chooseinstead.

如果您想要这里的“else”案例,您可以使用xsl:choose代替。

回答by Ondra ?i?ka

Check this: https://github.com/wildfly/wildfly/blob/master/testsuite/integration/src/test/xslt/enableTrace.xsl

检查这个:https: //github.com/wildfly/wildfly/blob/master/testsuite/integration/src/test/xslt/enableTrace.xsl

<xsl:template match="//l:subsystem/l:periodic-rotating-file-handler" use-when="$trace">
    <xsl:choose>
        <xsl:when test="$trace='none'">
            ...
        </xsl:when>
        <xsl:otherwise>
            ...
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Apply that to your code...

将其应用于您的代码...

回答by Dimitre Novatchev

<xsl:variable   name='person' 
    select='pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename)'>

</xsl:variable>
<xsl:variable   name='person' 
    select='pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename)'>

</xsl:variable>

This is syntactically illegal XPath -- both 1.0 and 2.0. A location step cannot start with a predicate. The offending substring is: /[.

这是语法上非法的 XPath - 1.0 和 2.0。位置步骤不能以谓词开头。有问题的子串是:/[

Another syntax error(this time XML-well-formedness one) is that the <xsl:variable>element quoted above is not closed.

另一个语法错误(这次是 XML 格式错误)是<xsl:variable>上面引用的元素没有关闭。

You need to correct this.

你需要纠正这个。

Besides this, here is an XSLT 2.0 solution (with the syntax of XPath expression quoted above and of the <xsl:variable>corrected:

除此之外,这里是一个 XSLT 2.0 解决方案(使用上面引用的 XPath 表达式的语法和<xsl:variable>更正的:

In XSLT 2.0:

在 XSLT 2.0 中:

<xsl:variable name="person" as="xs:string?" select=
  "'young'[current()/pr:all[@pr:name=current()/@cx:name]
                                            /pr:properties
                                               [@ls:middlename='cengie']
          ]"/>