【问题标题】:Reading the attribute value without specifying the node name in xsl读取属性值而不指定 xsl 中的节点名称
【发布时间】:2012-10-11 13:17:23
【问题描述】:

我有一个像这样的 xsl:variable,

<xsl:variable name="flag" 
       select="(/node1/@attr = 1) or (/node2/@attr = 1)"/>

是否可以在不指定节点名称的情况下读取属性值?所以我可以消除or 条件。基本上我将两种类型的 xml 提供给 xslt,node1node2 是根节点名称。

【问题讨论】:

    标签: xslt xpath


    【解决方案1】:

    使用

    /*/@attr = 1 or /*/@attr = 2
    

    如果您需要消除or 运算符,请执行

    contains(' 1 2 ', concat(' ', /*/@attr, ' '))
    

    在 XPath 2.0 (XSLT 2.0) 中,这可以进一步缩写为

    /*/@attr = (1, 2)
    

    【讨论】:

    • 有一个问题。这 /*/@attr 会检查该属性的所有子节点还是只检查根节点?
    • @Mark,只有 top 元素——根据问题的要求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多