简体   繁体   English

需要命名空间管理器或 XsltContext。 此查询具有前缀、变量或用户定义的函数

[英]Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function

I am trying to call SelectNode from XmlDocument class and trouble due to this error:我正在尝试从XmlDocument类调用SelectNode并且由于此错误而出现问题:

Namespace Manager or XsltContext needed.需要命名空间管理器或 XsltContext。 This query has a prefix, variable, or user-defined function.此查询具有前缀、变量或用户定义的函数。

My code:我的代码:

   public void Add(ref XmlDocument xmlFormat, String strName)
   {
        XmlDocument dom;
        XSLTemplate xsl = null;
        String strPath = "";
        XmlNodeList nl;
        XmlAttribute na;
        int n;

        nl = (XmlNodeList)xmlFormat.SelectNodes("//xsl:import/@href",nsm);
    }

and xsl:和 xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="stylesheets/r_adresetiket.xsl" />
    <xsl:template match="/">
        <xsl:call-template name="retouradres">
            <xsl:with-param name="_retouradres" select="data/adresetiket/_retouradres" />
            <xsl:with-param name="minofdir" select="data/adresetiket/afzendgegevens/afzendgegevens" />
            <xsl:with-param name="checked" select="data/adresetiket/LB" />
        </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

You have to add xsl namespace to XmlNamespaceManager :您必须将xsl命名空间添加到XmlNamespaceManager

var document = new XmlDocument();
document.Load(...);
var nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

var nl = document.SelectNodes("//xsl:import/@href", nsmgr);

The document can be traversed by GetElementsByTagName and it doesn't necessarily need using XmlNamespaceManager :该文档可以由GetElementsByTagName遍历,它不一定需要使用XmlNamespaceManager

var nodes = document.GetElementsByTagName("xsl:import");
var href =  nodes[0].Attributes["href"];

Fiddle小提琴

var document = new XmlDocument();
document.LoadXml(rawData);

var nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("cbc", "urn:xxx"); //for example
nsmgr.AddNamespace("cac", "urn:yyy");            

XmlElement xmlElem = document.DocumentElement;
var node = xmlElem.SelectSingleNode("cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[@schemeID='VKN']/text()", nsmgr);
var nodeText = node.InnerText;

在此处输入图片说明

All namespaces that will be used in XML should be added.应添加将在 XML 中使用的所有名称空间。
Then you can access the values of the relevant nodes using xpath .然后您可以使用xpath访问相关节点的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要命名空间管理器或XsltContext。 此查询具有前缀,变量或用户定义的函数。 在HTMLAgilityPack中 - Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function. in HTMLAgilityPack 需要HtmlAgilityPack命名空间管理器或XsltContext。 该查询具有前缀,变量或用户定义的函数 - HtmlAgilityPack Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function 需要命名空间管理器或 XsltContext。 此查询具有前缀、变量或用户定义的函数 - Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function System.Xml.XPath.XPathException:&#39;需要名称空间管理器或XsltContext。 该查询具有前缀,变量或用户定义的功能。” - System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.' 调用格式编号 XPath 函数时,收到错误消息:“需要命名空间管理器或 XsltContext。” - When calling format-number XPath function, I receive error: “Namespace Manager or XsltContext needed.” 需要命名空间管理器或 XsltContext - Namespace Manager or XsltContext needed 引发异常:需要名称空间管理器或XsltContext - Throw an exception: Namespace Manager or XsltContext needed Xpath无法解析XML:需要名称空间管理器或XsltContext - Xpath cannot parse XML : Namespace manager or XsltContext needed 读取XML时发生异常-“需要名称空间管理器或XsltContext” - Exception while reading XML - “Namespace Manager or XsltContext needed” 提供了XmlNamespaceManager,但仍然需要“Namespace Manager或XsltContext” - XmlNamespaceManager provided, but still get “Namespace Manager or XsltContext needed”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM