加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – xsl命名空间问题

发布时间:2020-12-16 23:31:43 所属栏目:百科 来源:网络整理
导读:如果我有这个XSL ?xml version="1.0" encoding="utf-8"?xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xsl:output method="html"/ xsl:output omit-xml-declaration="yes" encoding="UTF-8"/ xsl:template match='/' xsl:
如果我有这个XSL

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

    <xsl:template match='/'>
      <xsl:value-of select="//Description" />
    </xsl:template>
</xsl:stylesheet>

而这个XML

<ArrayOfLookupValue xmlns="http://switchwise.com.au/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <LookupValue>
    <Description>AGL</Description>
    <Value>8</Value>
  </LookupValue>
  <LookupValue>
    <Description>Australian Power &amp; Gas</Description>
    <Value>6</Value>
  </LookupValue>
  <LookupValue>
    <Description>EnergyAustralia</Description>
    <Value>13</Value>
  </LookupValue>
  <LookupValue>
    <Description>Origin Energy</Description>
    <Value>9</Value>
  </LookupValue>
  <LookupValue>
    <Description>TRU Energy</Description>
    <Value>7</Value>
  </LookupValue>
</ArrayOfLookupValue>

我如何从这一行实际获得一些数据:

<xsl:value-of select="//Description" />

我花了好几个小时就得到了这个结论,我得出的结论是xmlns = namespace是导致我悲痛的原因.

任何帮助非常感谢.

顺便说一句,XML来自一个Web服务,所以我不能只是“改变”它 – 我可以预处理它,但这并不理想……

此外,我已经确认从XML的模拟中删除命名空间确实解决了问题.

解决方法

这是XPath和XSLT最常见的FAQ.

简短的回答是,在XPath中,未加前缀的名称被认为属于“无名称空间”.但是,在具有默认命名空间的文档中,未加前缀的名称属于默认命名空间.

因此,对于这样的文件表达

//Description

什么都不选择(因为文档中没有描述(或任何其他)元素属于“无名称空间” – 所有元素名称都属于默认名称空间).

解:

在XSLT中定义一个名称空间,该名称空间与XML文档的默认名称空间具有相同的namespace-uri().然后使用如此定义的命名空间的前缀作为Xpath表达式中使用的任何名称:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://switchwise.com.au/">
    <xsl:output method="html"/>
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>

    <xsl:template match='/'>
      <xsl:copy-of select="//x:Description" />
    </xsl:template>
</xsl:stylesheet>

将此转换应用于提供的XML文档时:

<ArrayOfLookupValue xmlns="http://switchwise.com.au/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <LookupValue>
    <Description>AGL</Description>
    <Value>8</Value>
  </LookupValue>
  <LookupValue>
    <Description>Australian Power &amp; Gas</Description>
    <Value>6</Value>
  </LookupValue>
  <LookupValue>
    <Description>EnergyAustralia</Description>
    <Value>13</Value>
  </LookupValue>
  <LookupValue>
    <Description>Origin Energy</Description>
    <Value>9</Value>
  </LookupValue>
  <LookupValue>
    <Description>TRU Energy</Description>
    <Value>7</Value>
  </LookupValue>
</ArrayOfLookupValue>

产生了想要的正确结果:

<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>AGL</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>Australian Power &amp; Gas</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>EnergyAustralia</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>Origin Energy</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>TRU Energy</Description>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读