XSL selector where attribute can be upper or lower

2019-08-30 02:55发布

问题:

What is the XSL selector for a node value where an attribute can be upper or lower-case (or a combination)? Here is an example (I've stripped off much of the XML that isn't relevant). The first has an attribute of "NetworkID". The latter is "networkid". I need to get the value of the "Identity" node.

Attribute is "NetworkID".

<?xml version="1.0" encoding="UTF-8"?>
<cXML payloadID="1541780158582-6094783182107158181@216.109.111.67" timestamp="2018-11-09T08:15:58-08:00">
    <Header>
        <To>
            <Credential domain="networkid">
                <Identity>AN01000000000-T</Identity>
            </Credential>
        </To>
    </Header>
</cXML>

Attribute is "networkid".

<?xml version="1.0" encoding="UTF-8"?>
<cXML payloadID="1541780158582-6094783182107158181@216.109.111.67" timestamp="2018-11-09T08:15:58-08:00">
    <Header>
        <To>
            <Credential domain="NetworkID">
                <Identity>AN01000000000-T</Identity>
            </Credential>
        </To>
    </Header>
</cXML>

Is there a way to do this so that it ignores case?

<xsl:value-of select="Header/To/Credential[@domain='networkid']/Identity"/>

In my application, the above works in one case. I am forced to change it manually to get it to work in the other (but it breaks the prior).

回答1:

If you can use XSLT 2.0, then replace your:

<xsl:value-of select="Header/To/Credential[@domain='networkid']/Identity"/>

with:

<xsl:value-of select="Header/To/Credential[lower-case(@domain)='networkid']/Identity"/>

In XSLT 1.0, define:

<xsl:variable name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="lower-case" select="'abcdefghijklmnopqrstuvwxyz'"/>

then use:

<xsl:value-of select="Header/To/Credential[translate(@domain, $upper-case, $lower-case)='networkid']/Identity"/>


标签: xml xslt