XSLT从XML分配变量值

时间:2015-09-24 14:46:01

标签: xml xslt

来自XML文档:

<samlp:Response ID="_f9daea33-e32a-4dde-beb4-d5227690b1a3" Version="2.0"
    IssueInstant="2015-07-30T15:06:58.874Z"
    Destination="https://domain.net/Login/PAuthentication.aspx?configSet=SAML"
    xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
        >urn:jh:identityprovider</saml:Issuer>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
            <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
            <Reference URI="#_f9daea33-e32a-4dde-beb4-d5227690b1a3">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                    <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                        <InclusiveNamespaces PrefixList="#default samlp saml ds xs xsi"
                            xmlns="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    </Transform>
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>uL1LoegsT53UGJE/HQqG9VW1Mnc=</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>ifdW4P9/</SignatureValue>
        <KeyInfo>
            <X509Data>
                <X509Certificate>MIIHwzCCBaugAwIBAgIKeH+</X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>
    <samlp:Status>
        <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </samlp:Status>
    <saml:Assertion Version="2.0" ID="_b54ca592-4401-4107-a426-281918091842"
        IssueInstant="2015-07-30T15:06:58.898Z" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
        <saml:Issuer>urn:jh:identityprovider</saml:Issuer>
        <saml:Subject>
            <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"
                >ZQA|brandtest</saml:NameID>
            <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
                <saml:SubjectConfirmationData NotOnOrAfter="2015-07-30T16:06:59.331Z"
                    Recipient="https://jacksonhewitt.brandmuscle.net/Login/PAuthentication.aspx?configSet=SAML"
                 />
            </saml:SubjectConfirmation>
        </saml:Subject>
        <saml:Conditions NotBefore="2015-07-30T14:06:59.331Z"
            NotOnOrAfter="2015-07-30T16:06:59.331Z">
            <saml:AudienceRestriction>
                <saml:Audience>https://domain.net/Login/PAuthentication.aspx?configSet=SAML</saml:Audience>
                <saml:Audience>https://domain.net/</saml:Audience>
            </saml:AudienceRestriction>
        </saml:Conditions>
        <saml:AttributeStatement>
            <saml:Attribute Name="Phone"
                NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified">
                <saml:AttributeValue>867-5309</saml:AttributeValue>
            </saml:Attribute>
        </saml:AttributeStatement>
        <saml:AttributeStatement>
            <saml:Attribute Name="Phone2"
                NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified">
                <saml:AttributeValue>555-1212</saml:AttributeValue>
            </saml:Attribute>
        </saml:AttributeStatement>
        <saml:AuthnStatement AuthnInstant="2015-07-30T15:06:59.335Z">
            <saml:AuthnContext>
                <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef>
            </saml:AuthnContext>
        </saml:AuthnStatement>
    </saml:Assertion>
</samlp:Response>

XSLT文件:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
        xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xsl:template match="/">
    <AUTHENTICATOR>
        <USERINFO>
            <xsl:for-each select="/samlp:Response/*[local-name() = 'Assertion']/*[local-name() = 'AttributeStatement']/*">

            <!--This one works -->  
            <xsl:if test="(@Name='Phone')">
                <xsl:variable name="phone" select="*[local-name() = 'AttributeValue']" />
                <xsl:value-of select="$phone"/>
            </xsl:if>

            <!--This one doesn't work -->
            <xsl:variable name="phone2" select="samlp:AttributeValue[../@Name = 'Phone2']" />
            <xsl:value-of select="$phone2"/>

            </xsl:for-each>

            </USERINFO>
        </AUTHENTICATOR>
    </xsl:template>
</xsl:stylesheet>

尝试输出变量的值时,它为空。我猜测xsl:变量中的'select'语法是不正确的?

我能够像使用下面的代码一样工作,但我不想使用'if'。

    <xsl:if test="(@Name='Phone')">
       <xsl:variable name="phone" select="*[local-name() = 'AttributeValue']" />
       <xsl:value-of select="$phone"/>
    </xsl:if>

3 个答案:

答案 0 :(得分:1)

这里的关键问题是重点是什么。虽然answer of proycon显示您已使用属性轴(使用@),访问元素节点,并且忘记使用命名空间前缀,但如果未获取值,则焦点可能不在saml:Attribute。在没有看到其余代码的情况下,很难分辨它出错的地方。

假设你有这个:

<xsl:template match="Attribute">
   <!-- your variable here -->

然后模板永远不会匹配,因为它不在正确的命名空间中。但即使您将其修改为match="saml:Attribute",您的变量也会以*开头,child::*Name的缩写,而saml:Attribute下的属性<xsl:template match="saml:Xyz"> <!-- your variable here --> 没有Xyz 1}}。

假设你有这个:

xsl:template

然后您的重点是其他节点xsl:for-each

要解决此问题,您需要使用周围的焦点设置表达式(saml:Attribute<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="node() | @*"> <xsl:apply-templates /> </xsl:template> <xsl:template match="saml:Attribute[@Name = 'Phone']"> <xsl:variable name="phone" select="saml:AttributeValue" /> <xsl:value-of select="$phone" /> </xsl:template> </xsl:stylesheet> )来选择xsl:if的父级,或者如果焦点需要,则需要解决表达式本身是另一回事。以下内容将起作用,具体取决于样式表的其余部分(即,是否将模板应用于此节点,此处显示为浅跳过模式):

仅输出电话号码:

<xsl:if test="(@Name='Phone')">
   <xsl:variable name="phone" select="*[local-name() = 'AttributeValue']" />
   <xsl:value-of select="$phone"/>
</xsl:if>

更新(Q.更新后)

当我输入我的答案时,你说你不想使用local-name()并且你可以使用以下代码:

saml:AttributeValue

一些兴趣点:

  1. 除非无法绕过它,否则请不要使用xsl:stylesheet,如果直接NameTest就足够了,在这种情况下xsl:if/@test(不要忘记将命名空间添加到{{1} }根元素)
  2. xsl:if中的括号是多余的
  3. 你是对的,你不需要saml:AttributeValue[../@Name='Phone']。您可以简单地将变量更改为指向Phone,这将选择虚无,除非其父级在Name属性中具有<xsl:variable name="phone" select="saml:AttributeValue[../@Name = 'Phone']" /> 。换句话说,这是相同的:

    List should contain

答案 1 :(得分:0)

AttributeValue是一个元素而不是属性,尽管它具有误导性的名称;)所以使用*[@Name='Phone']/saml:AttributeValue

答案 2 :(得分:0)

如果由于任何原因您不想使用前缀并且不想使用 if ,则可以使用以下内容:

        <xsl:variable name="phone" select="*[@Name='Phone']/*[local-name() = 'AttributeValue']" />
        <xsl:value-of select="$phone"/>
相关问题