【问题标题】:Cannot transform from one xml file to another xml via xslt无法通过 xslt 从一个 xml 文件转换为另一个 xml
【发布时间】:2016-11-08 21:50:30
【问题描述】:

我正在尝试使用 xslt 将 xml 从一个 xml 转换为另一个,但无法得到错误。

我的输入是:

<?xml version = "1.0"?> 
<queryResponse xmlns="urn:sfobject.sfapi.successfactors.com" xmlns:ns2="urn:fault.sfapi.successfactors.com">
    <result>
        <sfobject>
            <id>1791
            </id>
            <type>CompoundEmployee
            </type>
            <execution_timestamp>2016-11-08T06:38:48.000Z
            </execution_timestamp>
            <version_id>1611P0
            </version_id>
        </sfobject>     
        <sfobject>
            <id>122
            </id>
            <type>Simple
            </type>
            <execution_timestamp>2016-11-08T08:32:18.000Z
            </execution_timestamp>
            <version_id>16120
            </version_id>
        </sfobject>
        <numResults>1
        </numResults>
        <hasMore>true
        </hasMore>
        <querySessionId>5f619648-548a-43ec-8119-627094f927a5
        </querySessionId>
    </result>
</queryResponse>

XSLT 是:

<?xml version = "1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:template match="result">
        <calls>
            <xsl:for-each select = "sfobject">
                <call>
                    <id>
                        <xsl:value-of select = "id"/>
                    </id>
                    <type>
                        <xsl:value-of select = "type"/>
                    </type>
                </call>
            </xsl:for-each>     
        </calls>
    </xsl:template>
</xsl:stylesheet>

我收到如下输出:

1791 CompoundEmployee 2016-11-08T06:38:48.000Z 1611P0 122 简单
2016-11-08T08:32:18.000Z 16120 1 对
5f619648-548a-43ec-8119-627094f927a5

基本上,我没有得到标签。我打算得到如下输出:

<calls>
  <call>
    <id>123</id>
    <type>CompoundEmployee</type>
  </call>
</calls>

【问题讨论】:

  • 我将&lt;call&gt; 标记更改为正确的结束标记&lt;/call&gt;。这是复制/粘贴错误吗?
  • 就像你之前的一千个 SO 用户一样,你已经陷入了默认命名空间陷阱。
  • 是的,结束标签是复制粘贴错误。

标签: xml xslt


【解决方案1】:

您需要在 xslt 文件中提供命名空间并使用命名空间前缀访问您的元素

你的 xslt 文件应该是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
               xmlns:sf="urn:sfobject.sfapi.successfactors.com"
               xmlns:sin="urn:fault.sfapi.successfactors.com" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
               exclude-result-prefixes="sf sin xs">
<xsl:output indent="yes" method="xml" />
   <xsl:template match="/sf:queryResponse">
      <calls>
         <xsl:for-each select="sf:result/sf:sfobject">
            <call>
               <id>
                  <xsl:value-of select="sf:id" />
               </id>
               <type>
                  <xsl:value-of select="sf:type" />
               </type>
            </call>
         </xsl:for-each>
      </calls>
   </xsl:template>
</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    在你的 XML 的第一行

    <queryResponse xmlns="urn:sfobject.sfapi.successfactors.com" xmlns:ns2="urn:fault.sfapi.successfactors.com">
    

    您将 XML 默认命名空间设置为

    xmlns="urn:sfobject.sfapi.successfactors.com"

    这导致&lt;result&gt; 节点真正被视为

    <urn:sfobject.sfapi.successfactors.com:result>
    

    节点。所以你的 XSLT 中的以下行

    <xsl:template match="result">
    

    尝试匹配&lt;result&gt; 节点,但仅找到

    <urn:sfobject.sfapi.successfactors.com:result> 
    

    XML 文件中的节点。因此“不匹配”文本输出。

    删除

    xmlns="urn:sfobject.sfapi.successfactors.com"

    从您的 XML 文件的第一行开始就可以解决问题。

    【讨论】:

    • XSLT 语言可以处理任何格式良好的 XML 输入。无需删除命名空间声明或以任何其他方式修改输入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多