【问题标题】:How to find an Xml Element when it's prefixed with a namespace?以命名空间为前缀时如何查找 Xml 元素?
【发布时间】:2026-01-29 19:25:02
【问题描述】:

我有一个如下的 Xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ea:Stories ea:WWVersion="2.0" xmlns:aic="http://ns.adobe.com/AdobeInCopy/2.0" xmlns:ea="urn:SmartConnection_v3">
<ea:Story ea:GUID="D8BEFD6C-AB31-4B0E-98BF-7348968795E1" pi0="style=&quot;50&quot; type=&quot;snippet&quot; readerVersion=&quot;6.0&quot; featureSet=&quot;257&quot; product=&quot;8.0(370)&quot; " pi1="SnippetType=&quot;InCopyInterchange&quot;">
<ea:StoryInfo>
<ea:SI_EL>headline</ea:SI_EL>
<ea:SI_Words>4</ea:SI_Words>
<ea:SI_Chars>20</ea:SI_Chars>
<ea:SI_Paras>1</ea:SI_Paras>
<ea:SI_Lines>1</ea:SI_Lines>
<ea:SI_Snippet>THIS IS THE HEADLINE</ea:SI_Snippet>
<ea:SI_Version>AB86A3CA-CEBC-49AA-A334-29641B95748D</ea:SI_Version>
</ea:StoryInfo>
</ea:Story>
</ea:Stories>

如您所见,所有元素都有“ea:”,这是一个命名空间前缀。

我正在编写一个 XSLT 文件来显示 SI_Snippet 文本,即“这是标题”。

如何在 XSLT 文件中写入 xpath?它应该包含命名空间还是应该被排除?

//ea:Story[ea:SI_EL='headline']/ea:SI_Snippet or 
//Story[SI_EL='headline']/SI_Snippet

其实我用的在线工具都失败了:http://xslt.online-toolz.com/tools/xslt-transformation.php

所以应该有别的办法吗?

如果稍后,它如何知道要查看哪个命名空间?我应该在运行时将命名空间传递给 XslTransformer 吗?

【问题讨论】:

    标签: c# asp.net xml xslt xpath


    【解决方案1】:

    你应该在你的 XSLT 中声明命名空间,然后使用你给它的前缀:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:ea="urn:SmartConnection_v3">
        <xsl:template match="/">
           <xsl:value-of select="//ea:Story[ea:SI_EL='headline']/ea:SI_Snippet" />
        </xsl:template>
    
        <!-- ...  -->
    </xsl:stylesheet>
    

    注意根元素中的xmlns:ea="urn:SmartConnection_v3"。这很重要。

    【讨论】:

      【解决方案2】:

      尝试使用XDocument

      var xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
      <ea:Stories ea:WWVersion=""2.0"" xmlns:aic=""http://ns.adobe.com/AdobeInCopy/2.0"" xmlns:ea=""urn:SmartConnection_v3"">
      <ea:Story ea:GUID=""D8BEFD6C-AB31-4B0E-98BF-7348968795E1"" pi0=""style=&quot;50&quot; type=&quot;snippet&quot; readerVersion=&quot;6.0&quot; featureSet=&quot;257&quot; product=&quot;8.0(370)&quot; "" pi1=""SnippetType=&quot;InCopyInterchange&quot;"">
      <ea:StoryInfo>
      <ea:SI_EL>headline</ea:SI_EL>
      <ea:SI_Words>4</ea:SI_Words>
      <ea:SI_Chars>20</ea:SI_Chars>
      <ea:SI_Paras>1</ea:SI_Paras>
      <ea:SI_Lines>1</ea:SI_Lines>
      <ea:SI_Snippet>THIS IS THE HEADLINE</ea:SI_Snippet>
      <ea:SI_Version>AB86A3CA-CEBC-49AA-A334-29641B95748D</ea:SI_Version>
      </ea:StoryInfo>
      </ea:Story>
      </ea:Stories>";
      
      XDocument xdoc = XDocument.Parse(xml.ToString());
      XElement v = xdoc.Descendants().FirstOrDefault(x => x.Name.LocalName == "SI_Snippet");
      

      编辑

      XPathNavigator navigator = xmldDoc.CreateNavigator();
      XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
      ns.AddNamespace("ea", "urn:SmartConnection_v3");
      var v = xmlDoc.SelectSingleNode("//ea:SI_Snippet", ns);
      

      【讨论】:

      • 我明白你是否想尖叫!但我在这个项目中使用 .NET 1.1,所以 XDocument 不存在,需要在 XSLT 中使用 XPath。
      • @TheLight 根据 OP 我的回答很好;)除此之外,试试我的编辑?
      最近更新 更多