2011-08-23 169 views
5

如果我有这样的XSLXSL命名空间问题

<?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 =命名空间是什么导致我的悲伤。

任何帮助非常感谢。

者均基于XML是从Web服务来,所以我不能只是“改变” - 我可以是进行预处理,但不是理想......

我也已经证实,去除的命名空间XML的模拟确实解决了这个问题。

+0

好问题,+1。请参阅我的回答以获得解释和简短而简单的解决方案。 –

+0

你说得对,命名空间是有差别的。请参阅Dimitre的解释......如果您阅读了XML名称空间(特别是在XPath中使用的),您将真的节省自己的时间。 – LarsH

回答

12

这是XPath和XSLT的最常见问题解答

简短回答是在XPath中,前缀名被认为属于“no namespace”。但是,在具有默认名称空间的文档中,前缀名称属于默认名称空间。

因此,对于这样的原稿的表达

//Description 

选择什么(因为没有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> 

当该变换被应用到所提供的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> 
+0

+1好答案。您如何将问题标记为常见问题解答? – LarsH

+0

@LarsH:我认为没有办法将问题标记为常见问题 - 目前常见问题取决于他们的观点数量 - 实际上是“观看次数最多”。而且我们需要的不是常见问题解答蝙蝠FAT - 常见问题:) –

+0

大声笑 - 这很容易,当你知道如何!非常感谢您提供了这样一个简单易懂的答案,我已将这些更改放入我的XSL中,并且按照希望工作。再次感谢!! – Rob