[英]XSLT assign variable value from XML
从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:variable中的“选择”语法不正确?
我可以使它像使用下面的代码一样工作,但是我不想使用'if'。
<xsl:if test="(@Name='Phone')">
<xsl:variable name="phone" select="*[local-name() = 'AttributeValue']" />
<xsl:value-of select="$phone"/>
</xsl:if>
这里的关键问题是重点是什么。 尽管proycon的回答表明您已经使用属性轴(带有@
)来访问元素节点,并且忘记了使用名称空间前缀,但是如果没有获取值,则焦点可能不会放在saml:Attribute
。 如果不看其余的代码,很难分辨出哪里出错了。
假设您有:
<xsl:template match="Attribute">
<!-- your variable here -->
那么模板将永远不会被匹配,因为它不在正确的名称空间中。 但是,即使将其设置为match="saml:Attribute"
,变量也会以*
开头,它是child::*
缩写,并且在saml:Attribute
下没有具有Name
saml:Attribute
。
假设您有:
<xsl:template match="saml:Xyz">
<!-- your variable here -->
那么您的重点是其他一些节点Xyz
。
要解决此问题,您需要周围的焦点设置表达式( 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>
一些兴趣点:
local-name()
,在这种情况下,请使用saml:AttributeValue
(不要忘记将名称空间添加到xsl:stylesheet
根元素中) xsl:if/@test
中的括号是多余的 没错,您不需要xsl:if
。 您可以简单地将变量更改为指向saml:AttributeValue[../@Name='Phone']
,它将选择无,除非其父项在Name
属性中具有Phone
。 0.换句话说,这是相同的:
<xsl:variable name="phone" select="saml:AttributeValue[../@Name = 'Phone']" />
尽管AttributeValue名称具有误导性,但它是一个元素,而不是属性;)因此,请使用*[@Name='Phone']/saml:AttributeValue
如果出于任何原因您不想使用前缀,也不想使用if ,则可以使用以下命令:
<xsl:variable name="phone" select="*[@Name='Phone']/*[local-name() = 'AttributeValue']" />
<xsl:value-of select="$phone"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.