public static double ToDouble(XPathItem item) { XsltLibrary.CheckXsltValue(item); if (item.IsNode) { return(XPathConvert.StringToDouble(item.Value)); } Type itemType = item.ValueType; if (itemType == StringType) { return(XPathConvert.StringToDouble(item.Value)); } else if (itemType == DoubleType) { return(item.ValueAsDouble); } else { Debug.Assert(itemType == BooleanType, $"Unexpected type of atomic sequence {itemType}"); return(item.ValueAsBoolean ? 1d : 0d); } }
//------------------------------------------------------------------------ // ToBoolean (internal type to internal type) //------------------------------------------------------------------------ public static bool ToBoolean(XPathItem item) { XsltLibrary.CheckXsltValue(item); if (item.IsNode) { return(true); } Type itemType = item.ValueType; if (itemType == StringType) { return(item.Value.Length != 0); } else if (itemType == DoubleType) { // (x < 0 || 0 < x) == (x != 0) && !Double.IsNaN(x) double dbl = item.ValueAsDouble; return(dbl < 0 || 0 < dbl); } else { Debug.Assert(itemType == BooleanType, $"Unexpected type of atomic sequence {itemType}"); return(item.ValueAsBoolean); } }
public static bool ToBoolean(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 0) { return(false); } return(ToBoolean(listItems[0])); }
public static string ToString(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 0) { return(string.Empty); } return(ToString(listItems[0])); }
public static IList <XPathNavigator> ToNodeSet(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 1) { return(new XmlQueryNodeSequence(ToNode(listItems[0]))); } return(XmlILStorageConverter.ItemsToNavigators(listItems)); }
public static XPathNavigator ToNode(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 1) { return(ToNode(listItems[0])); } throw new XslTransformException(SR.Xslt_NodeSetNotNode, string.Empty); }
public static double ToDouble(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 0) { return(double.NaN); } return(ToDouble(listItems[0])); }
public static string ToString(XPathItem item) { XsltLibrary.CheckXsltValue(item); // Use XPath 1.0 rules to convert double to string if (!item.IsNode && item.ValueType == DoubleType) { return(XPathConvert.DoubleToString(item.ValueAsDouble)); } return(item.Value); }
//------------------------------------------------ // Msxml Extension Functions //------------------------------------------------ public static double MSNumber(IList <XPathItem> value) { XsltLibrary.CheckXsltValue(value); if (value.Count == 0) { return(Double.NaN); } XPathItem item = value[0]; string stringValue; if (item.IsNode) { stringValue = item.Value; } else { Type itemType = item.ValueType; if (itemType == XsltConvert.StringType) { stringValue = item.Value; } else if (itemType == XsltConvert.DoubleType) { return(item.ValueAsDouble); } else { Debug.Assert(itemType == XsltConvert.BooleanType, "Unexpected type of atomic value " + itemType.ToString()); return(item.ValueAsBoolean ? 1d : 0d); } } Debug.Assert(stringValue != null); double d; if (XmlConvert.TryToDouble(stringValue, out d) != null) { d = double.NaN; } return(d); }
//------------------------------------------------------------------------ // EnsureXXX methods (TreatAs) //------------------------------------------------------------------------ public static IList <XPathNavigator> EnsureNodeSet(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 1) { XPathItem item = listItems[0]; if (!item.IsNode) { throw new XslTransformException(SR.XPath_NodeSetExpected, string.Empty); } if (item is RtfNavigator) { throw new XslTransformException(SR.XPath_RtfInPathExpr, string.Empty); } } return(XmlILStorageConverter.ItemsToNavigators(listItems)); }
//------------------------------------------------ // EXslt Functions //------------------------------------------------ public static string EXslObjectType(IList <XPathItem> value) { if (value.Count != 1) { XsltLibrary.CheckXsltValue(value); return("node-set"); } XPathItem item = value[0]; if (item is RtfNavigator) { return("RTF"); } else if (item.IsNode) { Debug.Assert(item is XPathNavigator); return("node-set"); } object o = item.TypedValue; if (o is string) { return("string"); } else if (o is double) { return("number"); } else if (o is bool) { return("boolean"); } else { Debug.Fail("Unexpected type: " + o.GetType().ToString()); return("external"); } }
//------------------------------------------------------------------------ // ToNode (internal type to internal type) //------------------------------------------------------------------------ public static XPathNavigator ToNode(XPathItem item) { XsltLibrary.CheckXsltValue(item); if (!item.IsNode) { // Create Navigator over text node containing string value of item XPathDocument doc = new XPathDocument(); XmlRawWriter writer = doc.LoadFromWriter(XPathDocument.LoadFlags.AtomizeNames, string.Empty); writer.WriteString(ToString(item)); writer.Close(); return(doc.CreateNavigator()); } RtfNavigator?rtf = item as RtfNavigator; if (rtf != null) { return(rtf.ToNavigator()); } return((XPathNavigator)item); }