【问题标题】:XML default namespace and XSLT transfornation trouble in C#C# 中的 XML 默认命名空间和 XSLT 转换问题
【发布时间】:2012-06-28 19:22:08
【问题描述】:

在附言中在这篇文章中,您可以找到一个 C# 代码 sn-p,它使用 XSLT 转换 + RegEx 匹配从 XML 文档中获取货币汇率值。请阅读我的问题内联引用

//+

...

//-

评论行组。

谢谢。

附:代码:

   string currencyCode = "RUB";
var xml = new StringReader( 
    @"<gesmes:Envelope 
        xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01' 
        xmlns{{EmptyNameSpace}} ='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'>
        <gesmes:subject>Reference rates</gesmes:subject>
        <gesmes:Sender>
        <gesmes:name>European Central Bank</gesmes:name>
    </gesmes:Sender>
    <Cube>
        <Cube time='2012-06-27'>
        <Cube currency='USD' rate='1.2478' />
        <Cube currency='RUB' rate='41.1252' />
        <Cube currency='ZAR' rate='10.4601' />
        </Cube>
    </Cube>
    </gesmes:Envelope>"
            .Replace("{{EmptyNameSpace}}", ":ns1")
            //+
            // I'd like to get this code working the same way as it does now
            // producing
            //
            // Result: EUR/RUB => 41.1252
            //
            // output but when the above code line will be commented
            // and the below code line uncommented
            //-
            //.Replace("{{EmptyNameSpace}}", "") 
);

var xslt = new XmlTextReader(new StringReader(
@"<xsl:stylesheet version='2.0' 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
    xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01' 
    xmlns:ns1 ='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'
    >

<xsl:variable name='x1' select='gesmes:Envelope/Cube/Cube/Cube[@currency=""{0}""]' />
<xsl:template match='gesmes:Envelope'>
  [<xsl:apply-templates select='$x1' />]
</xsl:template>
<xsl:template match='Cube'>
    <xsl:value-of select='@rate' />
</xsl:template>
</xsl:stylesheet>".Replace("{0}", currencyCode)  

));

var xDoc = new XPathDocument(xml);
var xTr = new System.Xml.Xsl.XslCompiledTransform(); 
xTr.Load(xslt) ;

StringBuilder sb = new StringBuilder(); 
StringWriter writer = new StringWriter(sb); 
xTr.Transform(xDoc, null, writer);

string pattern = @"\[(?'name'[0-9]*\.[0-9]*)\]";

System.Console.WriteLine(
    "Result: EUR/{0} => {1}",
    currencyCode, 
    Regex.Match(sb.ToString(), pattern)
   .Groups["name"].Value);    

// Result: EUR/RUB => 41.1252

【问题讨论】:

    标签: c# xml xslt transformation xml-namespaces


    【解决方案1】:

    据我查看该代码可知,您的问题在于处理 XPath 中的默认命名空间:

    • 只要{{EmptyNameSpace}} 是:ns1,一切都很好——在您的Xml 文档中,定义了前缀为gesmes 和ns1 的命名空间http://www.gesmes.org/xml/2002-08-01 和http://www.ecb.int/vocabulary/2002-08-01/eurofxref;您的 Xml 文档使用标识符,例如来自 gesmes 的 &lt;gesmes:Envelope&gt; 和其他标识符,即 &lt;Cube&gt;,它们与任何命名空间无关。在 XSLT 代码中,您正在使用 XPath 表达式,例如(此处缩写为)gesmes:Envelope/Cube/Cube/Cube,这很好,因为此表达式引用了以 gesmes 前缀声明的命名空间中的 &lt;Envelope&gt; 元素,以及&lt;Cube&gt; 没有命名空间的元素。
    • 将{{EmptyNameSpace}} 设置为空字符串后,情况会发生变化:现在,http://www.ecb.int/vocabulary/2002-08-01/eurofxref 是文档的默认命名空间。因此,文档中的 &lt;Cube&gt; 元素被视为属于该命名空间。但是,您的 XPath 表达式仍将出现在 XPath 中的 &lt;Cube&gt; 元素视为没有命名空间 - 它们没有任何命名空间前缀,并且 XPath 不知道默认命名空间。

    作为一种解决方案,您应该在 XPath 表达式中添加命名空间前缀 - 如果 &lt;Cube&gt; 元素属于 http://www.ecb.int/vocabulary/2002-08-01/eurofxref 命名空间,您的 XPath 应该如下所示:

    gesmes:Envelope/ns1:Cube/ns1:Cube/ns1:Cube
    

    (当然,您的 XSLT 中的其他 XPath 表达式必须进行相应调整。)

    如果您需要任何进一步的解释,或者我的假设是否走错了路,请告诉我。

    更新:如果 Xml 文档具有默认命名空间,XSLT 应该如下所示:

    <xsl:stylesheet version="2.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01' xmlns:ns1='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'>
    
        <xsl:variable name='x1' select='gesmes:Envelope/ns1:Cube/ns1:Cube/ns1:Cube[@currency="RUB"]'/>
    
        <xsl:template match='gesmes:Envelope'>
            [<xsl:apply-templates select='$x1'/>]
        </xsl:template>
    
        <xsl:template match='ns1:Cube'>
            <xsl:value-of select='@rate'/>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 感谢您的评论。如果我通过将{{EmptyNameSpace}} 设置为一个空字符串(通过注释一个代码行并取消注释另一行)使http://www.ecb.int/vocabulary/2002-08-01/eurofxref 成为默认命名空间,那么建议的XPath 表达式gesmes:Envelope/ns1:Cube/ns1:Cube/ns1:Cube 将不会产生任何结果。我错过了应该是 http://www.ecb.int/vocabulary/2002-08-01/eurofxref 作为默认命名空间的 XPath 表达式 - 一个 XPath 表达式导致的输出与当前的原始代码相同。谢谢。
    • 顺便说一句,我已经准备好了,我正在运行LINQPad 中的原始代码 sn-p,唯一的附加参考集(通过 F4 -> 添加...)到 System.XML.dll.. .
    • @ShamilS:我添加了 XSLT 应该是什么样子的示例。
    • 非常感谢!现在可以了。你知道为什么在 XSL 文件中我必须使用命名空间 xmlns:ns1 ='http://www.ecb.int/vocabulary/2002-08-01/eurofxref 而在要转换的源 XML 文件中使用默认(未命名)命名空间 xmlns ='http://www.ecb.int/vocabulary/2002-08-01/eurofxref' 吗? AFAIS 我可以在 XSL 中使用任何名称来命名源 XML 的默认名称空间,然后在 XPath 表达式中使用该名称:在 XSL 规范中描述/解释了命名(约定)的位置?对我来说看起来不合逻辑,这种奇怪的命名规则是什么原因?谢谢。
    • @ShamilS:XPath 语言没有不带前缀的命名空间的概念。 XPath 中没有前缀的标识符总是被认为没有命名空间。参看。例如this posting,它表示:在 XPath 中无法定义空前缀以映射到某个命名空间 URI。与 Xml 语言相反,XPath 语言不支持它。
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2010-12-16
    • 2015-07-23
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多