获取 Cordova 的 config.xml 文件的 XPath

4
使用Cordova的config.xml,其默认文件如下:
<?xml version='1.0' encoding='utf-8' ?>
<widget id="io.cordova.hellocordova" version="0.0.1" xmlns="http://www.w3.org/ns/widgets"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>HelloCordova</name>
  <description>
    A sample Apache Cordova application that responds to the deviceready event.
  </description>
  <author email="dev@cordova.apache.org" href="http://cordova.io">
    Apache Cordova Team
  </author>
  <content src="index.html" />
  <plugin name="cordova-plugin-whitelist" version="1" />
  <access origin="*" />
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />
  <platform name="android">
    <allow-intent href="market:*" />
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
  </platform>
</widget>

使用gulp-xml-editor,我试图创建一个插件来编辑name的值,但无论我如何尝试,都找不到正确的元素xpath。我尝试了以下选项:.//*[name].//*/widgets:name//name,均未成功。有任何想法吗?

非常感兴趣那个解决方案。我正在使用 gulp 中的“replace”从配置文件中进行替换(gulp.src(['config.xml']).pipe(replace('project_name', config.project_name))),其中 project_name 是 <name>project_name</name>。 - aorfevre
1个回答

5
元素<name>位于默认命名空间xmlns="http://www.w3.org/ns/widgets"中。通常在xpath中,要选择命名空间中的元素,需要注册一个指向命名空间URI的前缀,并在xpath中使用该前缀。
我不了解gulp-xml-editor,但在GitHub页面you linked中有一个非常相似的示例,特别是被注释为“使用命名空间使用用户特定对象编辑XML文档”。
/*
  edit XML document by using user specific object using a namespace
*/
gulp.src("./manifest.xml")
  .pipe(xeditor([
    {path: '//xmlns:name', text: 'new names'},
  ], 'http://www.w3.org/ns/widgets'))
  .pipe(gulp.dest("./dest"));

2
太好了,我意识到我错过的是在数组后面传递命名空间。感谢您的回复。 - mhartington
1
感谢您的回答。我从未知道XPath获取实际上是如何工作的。 - AdityaSaxena

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接