17

I have a simple requirement where in I need to fetch the value of attribute xml:id i.e af1. I am making use of a SAXParser and here is my xpath:a/aff/@xml:id on the contrary I was able to fetch value of using the xpath:a/aff/@value.

But i was unable to retrieve the value could you please help me?

<?xml version="1.0" encoding="UTF-8" ?>
<a>
   <aff xml:id="af1" value="a">
        <uAff>
            Hello
        </uAff>
    </aff>
    <aff xml:id="corr1">
        <uAff>
            Hello1
        </uAff>
    </aff>
</a>

Thanks in advance.

3 Answers 3

28

To get the value of the attributes you could use:

/a/aff/@*[name()='xml:id']
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot :-) Your answer was SPOT ON :-) Can u please tell how to get value of the tag which has colon.Say instead of aff if it was aff:a, how can we parse it?
if you have aff:a then aff must be a namespace, and that is a different problem. have a look here: stackoverflow.com/questions/536441/…
or you can use local-name() instead of name(), FWIW stackoverflow.com/a/11131700/32453
I am not an expert on xpath but it seems if you reach an attribute you can always access its parent element by adding a /.. [slash double dot] at the end i.e. : "//@*[name()='xml:id' and .='corr1']/.."
1

/a/aff/@xml:id works just fine in getting the values...

Are you trying to get both values?

If you are trying to get just the first value you could use /a/aff[1]/@xml:id

1 Comment

Your answer is correct if there is no colon, but when there is a colon it doesnt fetch the attribute value with /a/aff/@xml:id this xpath.
0

@tibtof's solution worked for my complex Cordova config.xml transformation using gulp and gulp-xml-transformer to fix the ionic-plugin-deeplinks AndroidManifest.xml implementation by targeting the android:host attribute like so since the colon in the attribute name would not work with path: '//xmlns:platform[@name="android"]/xmlns:config-file/xmlns:intent-filter/xmlns:data[@android:host]':

gulpfile.js:

var gulp = require('gulp'),
    xeditor = require("gulp-xml-transformer");

function config(avc, bundleId, ioscfbv, version, appLabel, hostname, done) {
  gulp.src('config.xml')
    .pipe(xeditor([
        {
            path: '//xmlns:widget',
            attr: { 'android-versionCode': avc, 'id': bundleId, 'ios-CFBundleVersion': ioscfbv, 'version': version }
        },
        {
            path: '//xmlns:name',
            text: appLabel
        },
        {
            path: '//*[@name="Hostname"]',
            attr: { 'value': bundleId }
        },
        {
            path: '//*[@name="ios"]/xmlns:config-file/xmlns:array/xmlns:string',
            text: 'applinks:' + hostname
        },
        {
            path: '//xmlns:platform[@name="android"]/xmlns:config-file/xmlns:intent-filter/xmlns:data[@*[name()="android:host"]]',
            attr: { 'android:host': hostname }
        }
    ], 'http://www.w3.org/ns/widgets'))
    .pipe(gulp.dest('.'))
    .on('finish', done);
}

config.xml:

<widget xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0" android-versionCode="0" id="com.bundle.placeholder" ios-CFBundleVersion="1.0.0.0" version="1.0.0">
    ...
    <platform name="android">
        ...
        <config-file target="AndroidManifest.xml" parent="/manifest/application/activity">
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="https"/>
                <data android:host="com.bundle.placeholder"/>
            </intent-filter>
        </config-file>
        ...
    </platform>
    ...
</widget>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.