Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Frends.Xml.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ public void TestXPathQueryResultsThatIsOfTypeXml()
Assert.That(((JToken)jTokenRes[2])["price"].Value<string>(), Is.EqualTo("9.99"));
}

[Test]
public void TestXPathQueryWithAdditionalNamespace()
{
const string xPath = "fn:round(/bookstore/book[@genre='novel']/price)";
var res = Frends.Xml.Xml.XpathQuery(new QueryInput() { Xml = Xml, XpathQuery = xPath }, new QueryOptions()
{
XmlNamespaces = new XmlNamespace[] {new XmlNamespace { Prefix = "fn", Uri = "http://www.w3.org/2005/xpath-functions" } }
});
var jTokenRes = res.ToJson();
Assert.That(res.Data[0], Is.EqualTo(12.0d));
Assert.That(((JToken)jTokenRes[0]).Value<float>(), Is.EqualTo(12.0d));
}

[Test]
public void TestXsltTransform()
{
Expand Down
21 changes: 21 additions & 0 deletions Frends.Xml/Definitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class QueryOptions
/// XPath Query language version
/// </summary>
public XPathVersion XpathVersion { get; set; }

/// <summary>
/// XML namespaces to declare
/// </summary>
public XmlNamespace[] XmlNamespaces { get; set; }
}

public class QueryResults
Expand Down Expand Up @@ -182,4 +187,20 @@ public class JsonToXmlInput
/// </summary>
public string XmlRootElementName { get; set; }
}

/// <summary>
/// XML namespace
/// </summary>
public class XmlNamespace
{
/// <summary>
/// Namespace prefix
/// </summary>
public string Prefix { get; set; }

/// <summary>
/// Namespace URI
/// </summary>
public string Uri { get; set; }
}
}
7 changes: 7 additions & 0 deletions Frends.Xml/Xml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ private static XPathSelector SetupXPathSelector(QueryInput input, QueryOptions o
builder.SchemaValidationMode = SchemaValidationMode.Lax;

var xPathCompiler = proc.NewXPathCompiler();
if (options.XmlNamespaces != null)
{
foreach (XmlNamespace ns in options.XmlNamespaces)
{
xPathCompiler.DeclareNamespace(ns.Prefix, ns.Uri);
}
}

switch (options.XpathVersion)
{
Expand Down