【问题标题】:xml transform XSLT with namespaces [duplicate]带有命名空间的 xml 转换 XSLT [重复]
【发布时间】:2018-06-27 11:34:14
【问题描述】:

我有一个从 SharePoint 生成的 xml,我需要提取一些数据并生成一个简化的 xml

这是我的原始(简化)源文件

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://itkm.gamesacorp.com/applications/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    <id>f8a19240-e319-454a-b4a6-fab8e3470c8d</id>
    <title />
    <updated>2018-01-17T15:13:15Z</updated>
    <entry m:etag="&quot;53&quot;">
        <id>Web/Lists(guid'fe244c05-99f9-4b69-8727-46327122b245')/Items(1)</id>
        <content type="application/xml">
            <m:properties>
                <d:Codigo>0002</d:Codigo>
                <d:App_x0020_name>GOT</d:App_x0020_name>
            </m:properties>
        </content>
    </entry>
    <entry m:etag="&quot;49&quot;">
        <id>Web/Lists(guid'fe244c05-99f9-4b69-8727-46327122b245')/Items(3)</id>
        <content type="application/xml">
            <m:properties>
                <d:Codigo>0006</d:Codigo>
                <d:App_x0020_name>ALTAIR</d:App_x0020_name>
            </m:properties>
        </content>
    </entry>
</feed>

这是我的 xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>

<xsl:template match="feed">
  <Applications>
      <xsl:for-each select="entry">
         <App>
             <Code><xsl:value-of select="content/properties/Codigo"/></Code>
             <Name><xsl:value-of select="content/properties/App_x0020_name"/></Name>
             <Uri><xsl:value-of select="id"/></Uri>
        </App>
      </xsl:for-each>
  </Applications>
</xsl:template>
</xsl:stylesheet>

如果我在 xml 和 xslt 中都省略了命名空间,结果符合预期,但我需要 xlst 使用命名空间,但我不知道 xslt 中包含哪些命名空间(以及如何包含)

这是适用于我的修改后的 xml,请注意我删除了对命名空间的所有引用:

<?xml version="1.0" encoding="utf-8"?>
<feed>
    <id>f8a19240-e319-454a-b4a6-fab8e3470c8d</id>
    <title />
    <updated>2018-01-17T15:13:15Z</updated>
    <entry etag="&quot;53&quot;">
        <id>Web/Lists(guid'fe244c05-99f9-4b69-8727-46327122b245')/Items(1)</id>
        <content type="application/xml">
            <m:properties>
                <Codigo>0002</Codigo>
                <App_x0020_name>GOT</App_x0020_name>
            </m:properties>
        </content>
    </entry>
</feed>

如何使用 xslt 来使用命名空间?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    您需要在 XSLT 中声明命名空间,然后在 xpath 表达式中的所有元素名称中使用相关的命名空间前缀

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:a="http://www.w3.org/2005/Atom" 
        xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
        xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
        version="1.0"
        exclude-result-prefixes="a d m">
    <xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="no"/>
    
    <xsl:template match="a:feed">
      <Applications>
          <xsl:for-each select="a:entry">
             <App>
                 <Code><xsl:value-of select="a:content/m:properties/d:Codigo"/></Code>
                 <Name><xsl:value-of select="a:content/m:properties/d:App_x0020_name"/></Name>
                 <Uri><xsl:value-of select="a:id"/></Uri>
            </App>
          </xsl:for-each>
      </Applications>
    </xsl:template>
    </xsl:stylesheet>
    

    请注意,在您的 XML 中,您有一个默认名称空间(没有前缀),但在 XSLT 中,我已将其分配给前缀 a,以便可以在 xpath 表达式中使用它。使用的前缀根本不需要匹配 XML。它是必须匹配的命名空间 URI。

    【讨论】:

    • 完美!这正是我想要的。谢谢!
    猜你喜欢
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多