stz184
stz184

Reputation: 383

Filter element by attribute with namespace in E4X

I have an XML like this:

<product xmlns="http://www.example-schame.org" product-id="5555555">
    <display-name xml:lang="x-default">Default name</display-name>
    <display-name xml:lang="en-GB">English Name</display-name>
    <display-name xml:lang="it-IT">Italian name</display-name>
</product>

I want to get the default name, e.g. this one with attribute xml:lang="x-default".

I tried to get it as

var name = Product["display-name"].(@["xml:lang"] == "x-default");

but it returns me undefined. Any ideas?

Upvotes: 0

Views: 349

Answers (1)

Zlatin Zlatev
Zlatin Zlatev

Reputation: 3098

You are missing namespaces.

  1. You need to have a default namespace, as you have one defined for the product element.
  2. You need to have a xml namespace, as the lang attribute is of this namespace

Here is some sample code

var product = <product xmlns="http://www.example-schame.org" product-id="5555555">
    <display-name xml:lang="x-default">Default name</display-name>
    <display-name xml:lang="en-GB">English Name</display-name>
    <display-name xml:lang="it-IT">Italian name</display-name>
</product>;

default xml namespace = product.namespace();
var xmlns = new Namespace("xml", "http://www.w3.org/XML/1998/namespace");
var name = product["display-name"].(@xmlns::lang == "x-default");

Upvotes: 0

Related Questions