2016-03-21 102 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定义。