获取cordova的config.xml的XPath

mha*_*ton 4 xml xpath cordova

使用cordovas confix.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>
Run Code Online (Sandbox Code Playgroud)

使用gulp-xml-editor我正在尝试创建一个插件来编辑值name,但无论我如何尝试,我都无法获得正确的xpath元素.

我试过的选项:

  • .//*[name]
  • .//*/widgets:name
  • //name

没有成功.知道它是什么?

har*_*r07 5

该元素<name>位于默认名称空间中xmlns="http://www.w3.org/ns/widgets".通常在xpath中,要选择命名空间中的元素,您需要注册指向命名空间uri的前缀,并在xpath中使用该前缀.

我不知道gulp-xml-editor,但在你链接的GitHub页面中似乎有一个非常接近的例子,特别是评论" 使用命名空间使用用户特定对象编辑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"));
Run Code Online (Sandbox Code Playgroud)

  • 完美,我意识到我错过的是在数组之后传递命名空间.谢谢回复 (2认同)