简体   繁体   中英

XmlDsigXPathTransform - namespace prefix is not defined

I am trying to create a signed xml detached signature file using this library: [opensbr]

I need to add an xpath filter to the TransformChain but upon calling SignedXml.ComputeSignature an exception is thrown that the namespace xbrli is not valid.

xpath: /xbrli:xbrl//*[not(local-name()='DocumentAdoptionStatus' or local-name()='DocumentAdoptionDate' and namespace-uri()='http://www.nltaxonomie.nl/8.0/basis/venj/items/bw2-data')]

constructing the transform (as per Microsoft example): public static XmlDsigXPathTransform CreateXPathTransform(string XPathString) { XmlDocument doc = new XmlDocument(); XmlElement xPathElem = doc.CreateElement("XPath"); xPathElem.InnerText = XPathString; XmlDsigXPathTransform xForm = new XmlDsigXPathTransform(); xForm.LoadInnerXml(xPathElem.SelectNodes(".")); return xForm; } public static XmlDsigXPathTransform CreateXPathTransform(string XPathString) { XmlDocument doc = new XmlDocument(); XmlElement xPathElem = doc.CreateElement("XPath"); xPathElem.InnerText = XPathString; XmlDsigXPathTransform xForm = new XmlDsigXPathTransform(); xForm.LoadInnerXml(xPathElem.SelectNodes(".")); return xForm; }

The xpath and xml file are both valid.

How can I use namespace prefixes with XmlDsigXPathTransform ?

MSDN example * suggests that you can declare namespace prefix on the XPath element :

.....
XmlElement xPathElem = doc.CreateElement("XPath");
xPathElem.SetAttribute("xmlns:xbrl", "xbrl namespace uri here");
xPathElem.InnerText = XPathString;
.....

*) See method LoadTransformByXml in Example #2

The issue here is that prefixes are only locally valid, within a limited scope. Your expression does not contain enough information to resolve your prefix to a namespace (even if it's the same as the default prefix in an XBRL document). One solution is to "feed" the namespace mapping in code, as suggested by har07.

Another solution is to include the namespace in the complete XPath fragment, on a node level. This is what is used by the Dutch audit profession in official business register filings.

<dsig-xpath:XPath xmlns:dsig-xpath="http://www.w3.org/2002/06/xmldsig-filter2" 
    xmlns:xbrli="http://www.xbrl.org/2003/instance" Filter="subtract">
    /xbrli:xbrl/*[localname()='DocumentAdoptionStatus' or local-name()='DocumentAdoptionDate' or local-name()='EmailAddressContact'] | //text()[normalize-space()='']
</dsig-xpath:XPath>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM