473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:myspace" xmlns="http://www.w3.org/1999/xhtml">
<ms:Locale id="EN" locale="en-US" xml:lang="EN">
<ms:Text name="LegalInformation">Legal Information</ms:Text>
<ms:Text name="Imprint">Imprint</ms:Text>
<ms:Text name="PrivacyPolicy">Privacy Policy</ms:Text>
</ms:Locale>
<ms:Locale id="DE" locale="de-DE" xml:lang="DE">
<ms:Text name="LegalInformation">Rechtlicher Hinweis</ms:Text>
<ms:Text name="Imprint">Impressum</ms:Text>
<ms:Text name="PrivacyPolicy">Datenschutz</ms:Text>
</ms:Locale>
</ms:Locales>

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

public XPathNodeIterator Local(string Key, CultureInfo Culture)
{
// Load document
XmlDocument locales=new XmlDocument();
locales.Load("Locales.xml");
XmlNamespaceManager nsmanager=new XmlNamespaceManager(locales.NameTable);
nsmanager.AddNamespace("ms", "urn:myspace");

// Create navigator
XPathNavigator navigator=locales.CreateNavigator();

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

// Create expression from string with namespaces
XPathExpression expression=navigator.Compile(Format);
expression.SetContext(nsmanager);

// Return iterator
return navigator.Select(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.Format(Search,
Culture.TwoLetterISOLanguageName.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 4268


Alexander Gräf wrote:

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


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

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

"Martin Honnen" <ma*******@yahoo.de> schrieb im Newsbeitrag
news:OV**************@TK2MSFTNGP14.phx.gbl...

I think case matters, at least in .NET's XSLT/XPath lang function so try
whether
Culture.TwoLetterISOLanguageName.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*******@yahoo.de> schrieb im Newsbeitrag
news:OV**************@TK2MSFTNGP14.phx.gbl...
I think case matters, at least in .NET's XSLT/XPath lang function so try
whether
Culture.TwoLetterISOLanguageName.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
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...
6
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
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
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...
3
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>...
11
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);...
1
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...
1
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...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.