/// <summary> /// Accept a node and an atomic value and return the sequence containing the string /// value of the node followed by the value. /// </summary> public static XdmValue combine(XdmNode node, XdmAtomicValue value) { IList <XdmItem> list = new List <XdmItem>(); list.Add(new XdmAtomicValue(node.StringValue)); list.Add(value); return(new XdmValue(list)); }
/// <summary> /// Accept a node and an atomic value and return the sequence containing the string /// value of the node followed by the value. /// </summary> public static XdmValue combine(XdmNode node, XdmAtomicValue value) { ArrayList list = new ArrayList(); list.Add(new XdmAtomicValue(node.StringValue)); list.Add(value); return(new XdmValue(list)); }
/*private static string getCanonicalPath(File file) { * try { * return file.getCanonicalPath(); * } catch (IOException err) { * return file.getAbsolutePath(); * } * }*/ private static QName getQNameAttribute(XPathCompiler xpath, XdmItem contextItem, string attributePath) { string exp = "for $att in " + attributePath + " return if (contains($att, ':')) then resolve-QName($att, $att/..) else QName('', $att)"; XdmAtomicValue qname = (XdmAtomicValue)xpath.EvaluateSingle(exp, contextItem); return(qname == null ? null : (QName)qname.Value); }
public XdmAtomicValueWrapper(XdmAtomicValue atomicValue) { if (atomicValue == null) { throw new ArgumentNullException("atomicValue"); } this.atomicValue = atomicValue; }
internal static QName GetQNameAttribute(XPathCompiler xpath, XdmItem contextItem, string attributePath) { string exp = "for $att in " + attributePath + " return if (contains($att, ':')) then resolve-QName($att, $att/..) else " + " if (contains($att,'{')) then QName(substring-before(substring-after($att,'{'),'}'),substring-after($att,'}')) else" + " QName('', $att)"; XdmAtomicValue qname = (XdmAtomicValue)xpath.EvaluateSingle(exp, contextItem); return(qname == null ? null : new QName(qname.ToString())); }
public Uri[] GetModules(String moduleUri, Uri baseUri, String[] locationHints) { XPathSelector xps = findModule.Load(); xps.SetVariable(new QName("", "testcase"), testCase); xps.SetVariable(new QName("", "moduleuri"), new XdmAtomicValue(moduleUri)); XdmAtomicValue s = (XdmAtomicValue)xps.EvaluateSingle(); return(new Uri[] { new Uri((String)s.Value) }); }
/** * Show a query producing a single atomic value as its result and returning the value * to the Java application */ public static void ExampleToSingleton() { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile("avg(for $i in 1 to 10 return $i * $i)"); XQueryEvaluator eval = exp.Load(); XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle(); Console.WriteLine("Result type: " + result.Value.GetType()); Console.WriteLine("Result value: " + (decimal)result.Value); }
public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context) { XdmAtomicValue p1 = arguments[0].AsAtomicValues().SingleOrDefault(); if (p1 == null) { return(EmptyEnumerator.INSTANCE); } return(Math.Acos((double)p1.Value).ToXdmAtomicValue().GetXdmEnumerator()); }
private bool assertEq(XdmNode assertion, SingleResultDoc result, XPathCompiler assertXpc) { if (IsException()) { return(false); } else { XPathSelector s = assertXpc.Compile("$result eq " + assertion.StringValue).Load(); s.SetVariable(new QName("result"), result.value); XdmAtomicValue item = (XdmAtomicValue)s.EvaluateSingle(); return(item != null && item.GetBooleanValue()); } }
public void AddParameter(string name, [NotNull] string value) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Argument is null or whitespace", nameof(name)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } var key = new QName(name); _externalParameters[key] = new XdmAtomicValue(value); }
public override XPathItem CreateAtomicValue(object value, XmlQualifiedName qualifiedName) { if (value == null) { throw new ArgumentNullException("value"); } if (qualifiedName == null) { throw new ArgumentNullException("qualifiedName"); } XdmAtomicValue atomicValue; XdmValue xdmValue = value.ToXdmValue(this); if (xdmValue.Count != 1) { throw new ArgumentException("value cannot be empty, or more than one item.", "value"); } try { XdmItem item = xdmValue.GetXdmEnumerator().AsItems().Single(); if (item.IsAtomic()) { atomicValue = (XdmAtomicValue)item; if (!qualifiedName.IsEmpty) { XmlQualifiedName typeName = atomicValue.GetTypeName(this.processor).ToXmlQualifiedName(); if (typeName != qualifiedName) { atomicValue = new XdmAtomicValue(value.ToString(), new QName(qualifiedName), this.processor); } } } else { atomicValue = (!qualifiedName.IsEmpty) ? new XdmAtomicValue(value.ToString(), new QName(qualifiedName), this.processor) : new XdmAtomicValue(value.ToString()); } } catch (Exception ex) { throw new SaxonException(ex.Message, ex); } return(atomicValue.ToXPathItem()); }
/** * Show a query that takes a parameter (external variable) as input. * The query produces a single atomic value as its result and returns the value * to the Java application. For the types of value that may be returned, and * their mapping to XPath data types, see {@link XPathEvaluator#Evaluate} */ public static void ExampleWithParam() { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); compiler.DeclareNamespace("p", "http://saxon.sf.net/ns/p"); XQueryExecutable exp = compiler.Compile( "declare variable $p:in as xs:integer external; $p:in * $p:in"); XQueryEvaluator eval = exp.Load(); eval.SetExternalVariable(new QName("http://saxon.sf.net/ns/p", "p:in"), new XdmAtomicValue(12)); XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle(); Console.WriteLine("Result type: " + result.Value.GetType()); Console.WriteLine("Result value: " + (long)result.Value); }
/// <summary> /// Compare two files using the XML comparator. /// </summary> /// <param name="actual">Filename of results obtained in this test run</param> /// <param name="gold">Filename of reference results (expected results)</param> /// <returns>true if the results are the same</returns> private bool compareXML(String actual, String gold) { try { XdmNode doc1 = processor.NewDocumentBuilder().Build(new Uri(actual)); XdmNode doc2 = processor.NewDocumentBuilder().Build(new Uri(gold)); XPathSelector t = compareDocuments.Load(); t.SetVariable(new QName("", "actual"), doc1); t.SetVariable(new QName("", "gold"), doc2); t.SetVariable(new QName("", "debug"), new XdmAtomicValue(debug)); XdmAtomicValue result = (XdmAtomicValue)t.EvaluateSingle(); return((bool)result.Value); } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine("***" + e.Message); return(false); } }
public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context) { XdmItem[] items = arguments[0].AsItems().ToArray(); XdmItem item; if (items.Length != 1 || (item = items[0]) is XdmNode) { return(Result("node-set")); } if (item.IsAtomic()) { XdmAtomicValue atomicVal = (XdmAtomicValue)item; object typedVal = atomicVal.Value; if (typedVal is string) { return(Result("string")); } switch (Type.GetTypeCode(typedVal.GetType())) { case TypeCode.Boolean: return(Result("boolean")); case TypeCode.Byte: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: case TypeCode.Single: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: return(Result("number")); } } return(Result("external")); }
/// <summary> /// Compare XML fragments /// </summary> /// <param name="actual">Actual results (the results, not the filename)</param> /// <param name="gold">Reference results (the results, not the filename)</param> /// <returns>true if equivalent</returns> private bool compareFragments(String actual, String gold) { try { String a = expandSpecialChars("<d>" + actual + "</d>"); String g = expandSpecialChars("<d>" + actual + "</d>"); XdmNode doc1 = processor.NewDocumentBuilder().Build( new XmlTextReader(new StringReader(a))); XdmNode doc2 = processor.NewDocumentBuilder().Build( new XmlTextReader(new StringReader(g))); XPathSelector t = compareDocuments.Load(); t.SetVariable(new QName("", "actual"), doc1); t.SetVariable(new QName("", "gold"), doc2); t.SetVariable(new QName("", "debug"), new XdmAtomicValue(debug)); XdmAtomicValue result = (XdmAtomicValue)t.EvaluateSingle(); return((bool)result.Value); } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine("***" + e.Message); return(false); } }
/// <summary> /// Add parameters to a transform /// </summary> /// <param name="parameterList"></param> /// <param name="xslProc"></param> private void AddParameters(XMLUtilities.XSLParameter[] parameterList) { if (parameterList != null) { foreach (XMLUtilities.XSLParameter rParam in parameterList) { var parameterName = new QName(rParam.Name); // Following is a specially recognized parameter name if (rParam.Name == "prmSDateTime") { var parameterValue = new XdmAtomicValue((new QName(XMLUtilities.GetCurrentDateTime(CultureToUse)))); m_transformer.SetParameter(parameterName, parameterValue); } else { var parameterValue = new XdmAtomicValue((new QName(rParam.Value))); m_transformer.SetParameter(parameterName, parameterValue); } } } }
/** * Show a query consisting of two modules, using a QueryResolver to resolve * the "import module" declaration */ public static void ExampleMultiModule() { String mod1 = "import module namespace m2 = 'http://www.example.com/module2';" + "m2:square(3)"; String mod2 = "module namespace m2 = 'http://www.example.com/module2';" + "declare function m2:square($p) { $p * $p };"; Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); InlineModuleResolver resolver = new InlineModuleResolver(); resolver.AddModule(new Uri("http://www.example.com/module2"), mod2); compiler.QueryResolver = resolver; XQueryExecutable exp = compiler.Compile(mod1); XQueryEvaluator eval = exp.Load(); XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle(); Console.WriteLine("Result type: " + result.Value.GetType()); Console.WriteLine("Result value: " + (long)result.Value); }
public static XPathItem ToXPathItem(this XdmAtomicValue value) { return(new XdmAtomicValueWrapper(value)); }
/** * Show how to get stylesheets that are associated with a given * xml document via the xml-stylesheet PI (see http://www.w3.org/TR/xml-stylesheet/). */ public static void ExampleUseAssociated(String sourceUri) { // Create a Processor instance. Processor processor = new Processor(); XsltExecutable exec; // Load the source document XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri)); Console.WriteLine("=============== source document ==============="); Console.WriteLine(input.OuterXml); Console.WriteLine("=========== end of source document ============"); // Navigate to the xml-stylesheet processing instruction having the pseudo-attribute type=text/xsl; // then extract the value of the href pseudo-attribute if present String path = @"/processing-instruction(xml-stylesheet)[matches(.,'type\s*=\s*[''""]text/xsl[''""]')]" + @"/replace(., '.*?href\s*=\s*[''""](.*?)[''""].*', '$1')"; XPathSelector eval = processor.NewXPathCompiler().Compile(path).Load(); eval.ContextItem = input; XdmAtomicValue hrefval = (XdmAtomicValue)eval.EvaluateSingle(); Console.WriteLine("evaluated"); String href = hrefval.ToString(); if (href == null || href == "") { Console.WriteLine("No suitable xml-stylesheet processing instruction found"); return; } else if (href[0] == '#') { // The stylesheet is embedded in the source document and identified by a URI of the form "#id" Console.WriteLine("Locating embedded stylesheet with href = " + href); String idpath = "id('" + href.Substring(1) + "')"; eval = processor.NewXPathCompiler().Compile(idpath).Load(); eval.ContextItem = input; XdmNode node = (XdmNode)eval.EvaluateSingle(); if (node == null) { Console.WriteLine("No element found with ID " + href.Substring(1)); return; } exec = processor.NewXsltCompiler().Compile(node); } else { // The stylesheet is in an external document Console.WriteLine("Locating stylesheet at uri = " + new Uri(input.BaseUri, href)); // Fetch and compile the referenced stylesheet exec = processor.NewXsltCompiler().Compile(new Uri(input.BaseUri, href.ToString())); } // Create a transformer XsltTransformer transformer = exec.Load(); // Run it transformer.SetParameter(new QName("", "", "a-param"), new XdmAtomicValue("hello to you!")); transformer.InitialContextNode = input; XdmDestination results = new XdmDestination(); transformer.Run(results); Console.WriteLine("1: " + results.XdmNode.StringValue); }