Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:17:57+00:00 2026-06-06T13:17:57+00:00

In P.S. of this posting you can find a C# code snippet, which uses

  • 0

In P.S. of this posting you can find a C# code snippet, which uses XSLT transformation + RegEx matching to get currency rate value from an XML document. Please read my question inline quoted by

//+

…

//-

comment lines group.

Thank you.

P.S. Code:

   string currencyCode = "RUB";
var xml = new StringReader( 
    @"<gesmes:Envelope 
        xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01' 
        xmlns{{EmptyNameSpace}} ='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'>
        <gesmes:subject>Reference rates</gesmes:subject>
        <gesmes:Sender>
        <gesmes:name>European Central Bank</gesmes:name>
    </gesmes:Sender>
    <Cube>
        <Cube time='2012-06-27'>
        <Cube currency='USD' rate='1.2478' />
        <Cube currency='RUB' rate='41.1252' />
        <Cube currency='ZAR' rate='10.4601' />
        </Cube>
    </Cube>
    </gesmes:Envelope>"
            .Replace("{{EmptyNameSpace}}", ":ns1")
            //+
            // I'd like to get this code working the same way as it does now
            // producing
            //
            // Result: EUR/RUB => 41.1252
            //
            // output but when the above code line will be commented
            // and the below code line uncommented
            //-
            //.Replace("{{EmptyNameSpace}}", "") 
);

var xslt = new XmlTextReader(new StringReader(
@"<xsl:stylesheet version='2.0' 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
    xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01' 
    xmlns:ns1 ='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'
    >

<xsl:variable name='x1' select='gesmes:Envelope/Cube/Cube/Cube[@currency=""{0}""]' />
<xsl:template match='gesmes:Envelope'>
  [<xsl:apply-templates select='$x1' />]
</xsl:template>
<xsl:template match='Cube'>
    <xsl:value-of select='@rate' />
</xsl:template>
</xsl:stylesheet>".Replace("{0}", currencyCode)  

));

var xDoc = new XPathDocument(xml);
var xTr = new System.Xml.Xsl.XslCompiledTransform(); 
xTr.Load(xslt) ;

StringBuilder sb = new StringBuilder(); 
StringWriter writer = new StringWriter(sb); 
xTr.Transform(xDoc, null, writer);

string pattern = @"\[(?'name'[0-9]*\.[0-9]*)\]";

System.Console.WriteLine(
    "Result: EUR/{0} => {1}",
    currencyCode, 
    Regex.Match(sb.ToString(), pattern)
   .Groups["name"].Value);    

// Result: EUR/RUB => 41.1252
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:17:59+00:00Added an answer on June 6, 2026 at 1:17 pm

    As far as I can tell from looking at that code, your problems is handling the default namespace in XPath:

    • As long as {{EmptyNameSpace}} is :ns1, everything’s fine – in your Xml document, the namespaces http://www.gesmes.org/xml/2002-08-01 and http://www.ecb.int/vocabulary/2002-08-01/eurofxref with the prefixes gesmes and ns1 are defined; your Xml document uses identifiers such as <gesmes:Envelope> from gesmes and others, namely <Cube>, that are not associated with any namespace. In the XSLT code, you are using XPath expressions, such as (here shortened) gesmes:Envelope/Cube/Cube/Cube, which is fine, as this expression refers to an <Envelope> element from the namespace declared with the gesmes prefix, as well as to <Cube> elements without a namespace.
    • Once you set {{EmptyNameSpace}} to an empty string, this changes: Now, http://www.ecb.int/vocabulary/2002-08-01/eurofxref is the default namespace of your document. Hence, the <Cube> elements in your document are considered to belong to that namespace. Your XPath expression, however, still considers the <Cube> elements that appear in the XPath as having no namespace – they do not have any namespace prefix and XPath doesn’t know about default namespaces.

    As a solution, you should add namespace prefixes to your XPath expression – if the <Cube> elements belong to the http://www.ecb.int/vocabulary/2002-08-01/eurofxref namespace, your XPath should read like this:

    gesmes:Envelope/ns1:Cube/ns1:Cube/ns1:Cube
    

    (Of course, the other XPath expressions in your XSLT have to be adapted accordingly.)

    Please let me know if you need any further explanations on this – or whether I’ve gotten on the wrong track with my assumptions.

    Update: Here’s what the XSLT should look like in order to work if the Xml document has the default namespace:

    <xsl:stylesheet version="2.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01' xmlns:ns1='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'>
    
        <xsl:variable name='x1' select='gesmes:Envelope/ns1:Cube/ns1:Cube/ns1:Cube[@currency="RUB"]'/>
    
        <xsl:template match='gesmes:Envelope'>
            [<xsl:apply-templates select='$x1'/>]
        </xsl:template>
    
        <xsl:template match='ns1:Cube'>
            <xsl:value-of select='@rate'/>
        </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel very very very silly posting this, but I can't find the answer.
I saw this posting which explained how to get BC3 working as the diff
I was looking at this posting: Get screen dimensions in pixels when I was
Posting this one for a friend. They have an Icefaces app that uses Icefaces's
I’m posting this because it took me ages to fix and I could find
I know this question has been asked, but I can't find more than one
I know this is a topic where you can find tons of answers for...
I get this error when posting a form. The strangest thing, though, is that
This should seem simple enough, but can't figure it out. I was porting a
I just looked at this posting: What is the best or most interesting use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.