473,802 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using XPath function lang() does not work in XPathNavigator

Hello,

I'm stuck with a simple problem, for which I don't have a solution. I
basically have an XML file containing fragments of plain text and html, in
several languages:

<?xml version="1.0" encoding="UTF-8" ?>
<ms:Locales xmlns:ms="urn:m yspace" xmlns="http://www.w3.org/1999/xhtml">
<ms:Locale id="EN" locale="en-US" xml:lang="EN">
<ms:Text name="LegalInfo rmation">Legal Information</ms:Text>
<ms:Text name="Imprint"> Imprint</ms:Text>
<ms:Text name="PrivacyPo licy">Privacy Policy</ms:Text>
</ms:Locale>
<ms:Locale id="DE" locale="de-DE" xml:lang="DE">
<ms:Text name="LegalInfo rmation">Rechtl icher Hinweis</ms:Text>
<ms:Text name="Imprint"> Impressum</ms:Text>
<ms:Text name="PrivacyPo licy">Datenschu tz</ms:Text>
</ms:Locale>
</ms:Locales>

Now I have written some C# to query the xml, searching for the language and
the name:

public XPathNodeIterat or Local(string Key, CultureInfo Culture)
{
// Load document
XmlDocument locales=new XmlDocument();
locales.Load("L ocales.xml");
XmlNamespaceMan ager nsmanager=new XmlNamespaceMan ager(locales.Na meTable);
nsmanager.AddNa mespace("ms", "urn:myspac e");

// Create navigator
XPathNavigator navigator=local es.CreateNaviga tor();

// Create and format search strings
const string
Search="/ms:Locales/ms:Locale[lang('{0}')]/ms:Text[@name='{1}']";
string Format=String.F ormat(Search,
Culture.TwoLett erISOLanguageNa me.ToUpper(), Key);

// Create expression from string with namespaces
XPathExpression expression=navi gator.Compile(F ormat);
expression.SetC ontext(nsmanage r);

// Return iterator
return navigator.Selec t(expression);
}

However, it does not work. It works if I replace one line into this:
const string
Search="/ms:Locales/ms:Locale[@xml:lang='{0}']/ms:Text[@name='{1}']";

I tried several things, and as a last check, I downloaded XML Spy from
Altova, loaded the Locales.xml and used the "Evaluate XPath" dialog to check
whats wrong:
/ms:Locales/ms:Locale[lang('DE')]/ms:Text[@name='Imprint']
This gives me - as expected - one ms:Text-node containing "Impressum" .

I simply don't know what I am doing wrong. lang() seems to evaluate to false
in any case, because this will return both language nodes:

// Create and format search strings
const string
Search="/ms:Locales/ms:Locale[not(lang('{0}') )]/ms:Text[@name='{1}']";
string Format=String.F ormat(Search,
Culture.TwoLett erISOLanguageNa me.ToUpper(), Key);

Is this a bug in the .NET Framework? I'm using v1.1.4322 with Visual Studio
2003 running on a german OS.

Thank you in advance,
Alexander Gräf

Nov 12 '05 #1
3 4288


Alexander Gräf wrote:

const string
Search="/ms:Locales/ms:Locale[lang('{0}')]/ms:Text[@name='{1}']";
string Format=String.F ormat(Search,
Culture.TwoLett erISOLanguageNa me.ToUpper(), Key);


I think case matters, at least in .NET's XSLT/XPath lang function so try
whether
Culture.TwoLett erISOLanguageNa me.ToLower()
improves things.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

"Martin Honnen" <ma*******@yaho o.de> schrieb im Newsbeitrag
news:OV******** ******@TK2MSFTN GP14.phx.gbl...

I think case matters, at least in .NET's XSLT/XPath lang function so try
whether
Culture.TwoLett erISOLanguageNa me.ToLower()
improves things.


Thanks. Sometimes this are the small things that make the difference.
Curiously enough, because xml:lang is defined with upper case letters. I
never thought of using lower case for lang(), mainly because it simply works
with upper case in my XSL files. So either the XPath or the XSL
implementation is wrong. However, thanks a lot for this simple solution.

Regards, Alexander
Nov 12 '05 #3


Alexander Gräf wrote:
"Martin Honnen" <ma*******@yaho o.de> schrieb im Newsbeitrag
news:OV******** ******@TK2MSFTN GP14.phx.gbl...
I think case matters, at least in .NET's XSLT/XPath lang function so try
whether
Culture.TwoLett erISOLanguageNa me.ToLower()
improves things.

Thanks. Sometimes this are the small things that make the difference.
Curiously enough, because xml:lang is defined with upper case letters. I
never thought of using lower case for lang(), mainly because it simply works
with upper case in my XSL files. So either the XPath or the XSL
implementation is wrong.


I think the XPath specification
<http://www.w3.org/TR/xpath#function-lang>
is pretty clear that comparison should ignore case so it looks like a
bug in .NET's XPath implementation.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2170
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows XP Pro SP 1, .Net Framework 1.1) and on our production server (Windows 2K SP 4, .Net Framework 1.1). I have simplified the code and data to isolate the problem. When I use the xsl:strip-space (Line 12) declaration in conjunction with the xsl:sort...
6
15513
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
5
9570
by: Paw Pedersen | last post by:
When you are "working" on a specific node from a XmlDocument instance, is it possible to get the full xpath to this node? Regards Paw
13
3248
by: David Thielen | last post by:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software" nav.SelectSingleNode"."); The select returns an exception: + $exception {"'.' has an invalid token."} System.Exception {System.Xml.XPath.XPathException} Any idea why? --
3
6808
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer> </Root> I would like to retreive "\Root\Customer\Name" out of it. Something like:
11
6468
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString); XPathNavigator nav = docNav.CreateNavigator(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<DocumentData></DocumentData>"); XmlElement elem = doc.CreateElement(currentNodeName);
1
5711
by: PAF | last post by:
Hi, I'm trying to evaluate a sum expression trougth .NET 2 System.Xml.XPath.XPathNavigator object. My code is : Dim document As New System.Xml.XmlDocument() document.LoadXml("<?xml version=""1.0""?><root><a>28.7</a><a>9.18</a><a>4.32</a></root>")
1
2790
by: =?Utf-8?B?amVycnlu?= | last post by:
I am trying to evaluate the Microsoft ms:utc() XPath extension in an XPath expression. When I do, I get an exception that I need an XsltContext. Given these are microsoft extentions, it seems unlikely that I would need to implement my own IXsltContextFunction class for microsoft published functions. Thus I suspect I am missing something. My Code looks as follows: this.XPathQueryString = this.textBoxQueryString.Text; XPathDocument...
5
4579
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using Visual C# window creating an application to read an xml file. At some point I need to use XPathNavigator and XPathNodeIterator however when I used those subjects I get error message . I add the "System.Xml.XPath" class however it either empty or not implemented. This problem happened also when I was going to use FlowLayoutPanel. How I can activate the above classes? Do I need to instal something? Thanks -- Nejadian
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.