XSLT转换文件空白输出

时间:2016-03-21 15:16:18

标签: xml xslt saml

我需要创建一个XSLT文件来将传入的SAML文件转换为XML。但我的转换文件没有创建任何输出,我不知道为什么?当我尝试将以下内容插入在线翻译工具时,并收到有关“模块中第5行的错误评估模板”的错误?

传入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>

2 个答案:

答案 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>

答案 1 :(得分:0)

这是您的solution

saml命名空间未在源xml中定义,但它是在xslt中定义的。