c# - Load xml and xslt from embedded resource in Saxon 9.4he -


i using saxon 9.4 home edition (saxon-he 9.4 .net) support xslt 2.0 , xpath 2.0, , xquery 1.0 in .net. code crashes when load files without uri.

  1. is possible load xml/xsl documents without uri related document loaded?
  2. if not, there way define uri elements embedded in dll-files?

any other solutions appreciated, term files must loaded within dll-file.

my code works long load xml/xsl file:

const string sourcepath = @"c:\test\testinvoicewitherror.xml"; const string xsltpath = @"c:\test\ubl-t10-biirules.xsl"; 

when try load embedded resource code throws exception stating 'no base uri supplied':

stream sourcestream = getembeddedresource("testproject1.testfiles.testinvoice.xml"); stream xsltstream = getembeddedresource("testproject1.testfiles.ubl-t10-biirules.xsl"); 

i have created uri's resources relative path throws exception 'this operation not supported relative uri.':

uri sourceuri = new uri("/testproject1;component/testfiles/testinvoice.xml",     urikind.relative); uri xslturi = new uri("/testproject1;component/testfiles/ubl-t10-biirules.xsl.xml", urikind.relative); 

here code:

using system; using system.collections.generic; using system.io; using system.reflection; using system.text; using system.xml; using microsoft.visualstudio.testtools.unittesting; using saxon.api;   namespace testproject1 {     [testclass]     public class xslttest     {         [testmethod]         public void saxontest()         {             stream sourcestream = getembeddedresource("testproject1.testfiles.testinvoice.xml");             stream xsltstream = getembeddedresource("testproject1.testfiles.ubl-t10-biirules.xsl");              uri sourceuri = new uri("/testproject1;component/testfiles/testinvoice.xml", urikind.relative);             uri xslturi = new uri("/testproject1;component/testfiles/ubl-t10-biirules.xsl.xml", urikind.relative);              const string sourcepath = @"c:\test\testinvoicewitherror.xml";             const string xsltpath = @"c:\test\ubl-t10-biirules.xsl";              processor processor = new processor();             xdmnode input = processor.newdocumentbuilder().build(new uri(sourcepath));              xslttransformer transformer = processor.newxsltcompiler().compile(new uri(xsltpath)).load();              transformer.initialcontextnode = input;              serializer serializer = new serializer();             stringbuilder sb = new stringbuilder();             textwriter writer = new stringwriter(sb);             serializer.setoutputwriter(writer);              transformer.run(serializer);              xmldocument xmldocout = new xmldocument();             xmldocout.loadxml(sb.tostring());             xmlnodelist failedasserts = xmldocout.selectnodes("/svrl:schematron-output/svrl:failed-assert",xmlinvoicenamespacemanager());              if (failedasserts == null)                 return;              foreach (xmlnode failedassert in failedasserts)             {                 if (failedassert.attributes == null)                     continue;                  xmlattribute typeoferror = failedassert.attributes["flag"];                  if (typeoferror.value.equals("warning"))                 {/*log something*/}                 else if (typeoferror.value.equals("fatal"))                 {/*log something*/}             }         }          private xmlnamespacemanager xmlinvoicenamespacemanager()         {             idictionary<string, string> list = new dictionary<string, string>                                                    {                                                        {"xml", "http://www.w3.org/xml/1998/namespace"},                                                        {"xsi", "http://www.w3.org/2001/xmlschema-instance"},                                                        {"xsd", "http://www.w3.org/2001/xmlschema"},                                                        {"udt","urn:un:unece:uncefact:data:specification:unqualifieddatatypesschemamodule:2"},                                                        {"qdt","urn:oasis:names:specification:ubl:schema:xsd:qualifieddatatypes-2"},                                                        {"ext","urn:oasis:names:specification:ubl:schema:xsd:commonextensioncomponents-2"},                                                        {"ccts", "urn:un:unece:uncefact:documentation:2"},                                                        {"cbc","urn:oasis:names:specification:ubl:schema:xsd:commonbasiccomponents-2"},                                                        {"cac","urn:oasis:names:specification:ubl:schema:xsd:commonaggregatecomponents-2"},                                                        {"inv", "urn:oasis:names:specification:ubl:schema:xsd:invoice-2"},                                                        {"svrl", "http://purl.oclc.org/dsdl/svrl"}                                                    };              xmlnametable xmlnametable = new nametable();              xmlnamespacemanager xmlinvoicenamespacemanager = new xmlnamespacemanager(xmlnametable);              foreach (keyvaluepair<string, string> ns in list)             {                 xmlinvoicenamespacemanager.addnamespace(ns.key, ns.value);             }             return xmlinvoicenamespacemanager;         }          protected static stream getembeddedresource(string path)         {             assembly asm = assembly.getexecutingassembly();             stream stream = asm.getmanifestresourcestream(path);             return stream;         }     } } 

i think can load stream saxon need set base uri first allow load references resources (like dtd in xml document or included or imported stylesheet modules). if sure don't have try e.g.

documentbuilder db = processor.newdocumentbuilder(); db.baseuri = new uri("file:///c:/");  xdmnode input = db.build(xsltstream); 

obviously if need resolve relative uris in xslt loaded embedded resource more work needed: need set xmlresolver class supports loading resource embedded resource, scheme of uris in xslt indicate resolver need load resource. don't think .net framework provides such kind of xmlresolver , uri class not support custom schema either.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -