【问题标题】:XSLT transform file blank outputXSLT 转换文件空白输出
【发布时间】:2016-03-21 15:16:18
【问题描述】:

我需要创建一个 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>

【问题讨论】:

  • 您的输入格式不正确:命名空间前缀saml 未定义。
  • 另外,您应该告诉我们您是否能够使用 XSLT 2.0 或者您是否受限于 XSLT 1.0

标签: xml xslt saml


【解决方案1】:

我怀疑无法产生任何输出是因为命名空间声明。您显示的输入文件没有命名空间声明这一事实可能表明您还没有意识到在处理此类问题时这些文件的重要性。

除此之外,您的代码看起来还可以,但非常冗长。这是一个较短的版本:

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

【讨论】:

    【解决方案2】:

    这是你的solution

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

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2016-10-29
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2013-01-07
      相关资源
      最近更新 更多