xsl命名空间问题

Rob*_*Rob 6 xml xslt xml-namespaces

如果我有这个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>
Run Code Online (Sandbox Code Playgroud)

而这个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>
Run Code Online (Sandbox Code Playgroud)

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

<xsl:value-of select="//Description" />
Run Code Online (Sandbox Code Playgroud)

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

任何帮助非常感谢.

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

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

Dim*_*hev 14

这是XPath和XSLT最常见的FAQ.

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

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

//Description
Run Code Online (Sandbox Code Playgroud)

什么都不选择(因为Description文档中没有(或任何其他)元素属于"无命名空间" - 所有元素名称都属于默认命名空间).

方案:

在XSLT中定义一个名称空间,namespace-uri()该名称空间与XML文档的默认名称空间相同.然后使用如此定义的命名空间的前缀作为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>
Run Code Online (Sandbox Code Playgroud)

将此转换应用于提供的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>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

<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>
Run Code Online (Sandbox Code Playgroud)