簡體   English   中英

XSLT:如果不為null,則串聯兩個字段值

[英]XSLT: concatenate two field values if not null

我需要修改現有的XML文檔以連接到字段值。 我不確定如何執行此操作,因為無法將變量保持在范圍內。

基本上,如果存在FirstName和LastName的值,則應輸出FirstName + LastName。 如果FirstName或LastName的一個或兩個值均為空,則不應輸出FullName。 請注意,這是一個現有文檔,我無法修改第34行以上的任何現有代碼。

這是我到目前為止的內容:

<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']/*">

                <xsl:if test="@Name='First_Name'">
                    <xsl:variable name="firstname" select="*[local-name() = 'AttributeValue']" />
                    <xsl:element name="field">  
                        <xsl:attribute name="name">FirstName</xsl:attribute>    
                        <xsl:attribute name="value">    
                            <xsl:for-each select="*[local-name() = 'AttributeValue']">  
                                <xsl:value-of select="normalize-space(current())"/> 
                            </xsl:for-each> 
                        </xsl:attribute> 
                    </xsl:element> 
                </xsl:if>

                <xsl:if test="@Name='Last_Name'">
                    <xsl:variable name="lastname" select="*[local-name() = 'AttributeValue']" />
                    <xsl:element name="field">  
                        <xsl:attribute name="name">LastName</xsl:attribute>     
                        <xsl:attribute name="value">    
                            <xsl:for-each select="*[local-name() = 'AttributeValue']">  
                                <xsl:value-of select="normalize-space(current())"/> 
                            </xsl:for-each> 
                        </xsl:attribute> 
                    </xsl:element> 
                </xsl:if>

                <!-- Unable to modify anything above this line-->
                <!-- if firstname & lastname not null, concatinate firstname + lastname-->
                <xsl:if test="not($firstname = '') or not($lastname = '')">
                    <xsl:element name="field">
                        <xsl:attribute name="name">FullName</xsl:attribute>
                        <xsl:attribute name="value">
                            <xsl:value-of select="concat($firstname,'+',$lastname)" />
                        </xsl:attribute>
                    </xsl:element>
                </xsl:if>

            </xsl:for-each>

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

下面是簡化的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>
<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:AttributeStatement>
        <saml:Attribute Name="FirstName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified">
            <saml:AttributeValue>John</saml:AttributeValue>
        </saml:Attribute>
    </saml:AttributeStatement>
            <saml:AttributeStatement>
        <saml:Attribute Name="LastName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified">
            <saml:AttributeValue>Doe</saml:AttributeValue>
        </saml:Attribute>
    </saml:AttributeStatement>
</saml:Assertion>

該示例非常令人困惑-不清楚什么是“記錄”以及您可以擁有多少記錄(根據XSLT判斷,只有一個?)。

看看是否可以以此為起點。 它假定每個saml:Assertion都是一條記錄,並且如果該記錄同時具有FirstName和LastName,則它將輸出FullName字段。

XSLT 1.0

<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:saml="urn:oasis:names:tc:SAML:2.0:assertion"
exclude-result-prefixes="samlp saml">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/samlp:Response">
    <AUTHENTICATOR>
        <xsl:for-each select="saml:Assertion">
            <USERINFO>
                <xsl:variable name="firstname" select="saml:AttributeStatement/saml:Attribute[@Name='FirstName']/saml:AttributeValue"/>
                <xsl:variable name="lastname" select="saml:AttributeStatement/saml:Attribute[@Name='LastName']/saml:AttributeValue"/>
                <xsl:if test="$firstname and $lastname">
                    <field name="FullName" value="{concat($firstname, ' ', $lastname)}"/>  
                </xsl:if>
            </USERINFO>         
        </xsl:for-each>
    </AUTHENTICATOR>        
</xsl:template>

</xsl:stylesheet>

測試輸入

<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
   <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
      <saml:AttributeStatement>
         <saml:Attribute Name="FirstName">
            <saml:AttributeValue>John</saml:AttributeValue>
         </saml:Attribute>
      </saml:AttributeStatement>
      <saml:AttributeStatement>
         <saml:Attribute Name="LastName">
            <saml:AttributeValue>Doe</saml:AttributeValue>
         </saml:Attribute>
      </saml:AttributeStatement>
   </saml:Assertion>
   <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
      <saml:AttributeStatement>
         <saml:Attribute Name="FirstName">
            <saml:AttributeValue>Madonna</saml:AttributeValue>
         </saml:Attribute>
      </saml:AttributeStatement>
   </saml:Assertion>
</samlp:Response>

結果

<?xml version="1.0" encoding="UTF-8"?>
<AUTHENTICATOR>
  <USERINFO>
    <field name="FullName" value="John Doe"/>
  </USERINFO>
  <USERINFO/>
</AUTHENTICATOR>

注意使用前綴來調出源XML中的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM