2016-03-21 61 views
0

我需要創建一個XSLT文件來將傳入的SAML文件轉換爲XML。但我的轉換文件沒有創建任何輸出,我不知道爲什麼?當我嘗試將以下內容插入到在線翻譯工具中,並獲得有關「模塊中第5行的模板錯誤評估模板」的錯誤?XSLT轉換文件空白輸出

傳入SAML:

<root> 
<saml:Attribute Name="State" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"> 
    <saml:AttributeValue>OR</saml:AttributeValue> 
</saml:Attribute> 
<saml:Attribute Name="Pilot_Item" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"> 
    <saml:AttributeValue>Skateboard</saml:AttributeValue> 
</saml:Attribute> 
<saml:Attribute Name="UserType" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"> 
    <saml:AttributeValue>Broker</saml:AttributeValue> 
</saml:Attribute> 
<saml:Attribute Name="Pilot_Item" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"> 
    <saml:AttributeValue>HandGlider</saml:AttributeValue> 
</saml:Attribute> 
<saml:Attribute Name="State" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"> 
    <saml:AttributeValue>CA</saml:AttributeValue> 
</saml:Attribute> 
</root> 

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:saml="urn:oasis:names:tc:SAML:2.0:assertion" 
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
<xsl:template match="/"> 
     <USERINFO> 
      <xsl:for-each select="//saml:Attribute[@Name='State']/saml:AttributeValue"> 
       <xsl:element name="field"> 
        <xsl:attribute name="name">State</xsl:attribute> 
        <xsl:attribute name="value"> 
         <xsl:value-of select="current()"/> 
         <xsl:if test="position()!=last()">,</xsl:if> 
        </xsl:attribute> 
       </xsl:element> 
      </xsl:for-each> 
      <xsl:for-each select="//saml:Attribute[@Name='Pilot_Item']/saml:AttributeValue"> 
       <xsl:element name="field"> 
        <xsl:attribute name="name">Custom1</xsl:attribute> 
        <xsl:attribute name="value"> 
         <xsl:value-of select="current()"/> 
         <xsl:if test="position()!=last()">,</xsl:if> 
        </xsl:attribute> 
       </xsl:element> 
      </xsl:for-each> 
      <xsl:for-each select="//saml:Attribute[@Name='UserType']/saml:AttributeValue"> 
       <xsl:element name="field"> 
        <xsl:attribute name="name">Group</xsl:attribute> 
        <xsl:attribute name="value"> 
         <xsl:value-of select="current()"/> 
         <xsl:if test="position()!=last()">,</xsl:if> 
        </xsl:attribute> 
       </xsl:element> 
      </xsl:for-each> 
     </USERINFO> 
</xsl:template> 
</xsl:stylesheet> 

需要的輸出:

<UserInfo> 
    <Custom1>Skateboard,HandGlider</Custom1> 
    <State>CA,OR</State> 
    <Group>Broker</Group> 
</UserInfo> 
+3

您的輸入格式不正確:名稱空間前綴'saml'未定義。 –

+0

另外,您應該告訴我們您是否能夠使用XSLT 2.0,或者您是否受限於XSLT 1.0 –

回答

0

我懷疑由於命名空間聲明導致輸出失敗。事實上,你顯示的輸入文件沒有名稱空間聲明可能表明你沒有意識到這些問題在解決這樣的問題時有多重要。

除此之外,你的代碼看起來不錯,但非常詳細。這裏有一個較短的版本:

<xsl:template match="/"> 
    <USERINFO> 
    <field name="State" 
      value="{string-join(//saml:Attribute[@Name='State']/saml:AttributeValue, ',')}"/> 
    <field name="Custom1" 
      value="{string-join(//saml:Attribute[@Name='Pilot_Item']/saml:AttributeValue, ',')}"/> 
    <field name="Group" 
      value="{string-join(//saml:Attribute[@Name='UserType']/saml:AttributeValue, ',')}"/> 
    </USERINFO> 
</xsl:template> 
0

這裏是你的solution

的SAML命名空間不是在源XML定義,但它是在你的XSLT定義。