简体   繁体   English

如何使用Google Apps脚本的XmlService设置XML元素的名称空间?

[英]How do you set the namespace of an XML element using Google Apps Script's XmlService?

I'm programmatically creating XML documents using Google Apps Script's XmlService . 我正在使用Google Apps Script的XmlService以编程方式创建XML文档。 However, I can't figure out how to add a namespace to an element. 但是,我不知道如何向元素添加名称空间。 How do I use the XmlService API to add a namespace to an individual XML element? 如何使用XmlService API向单个XML元素添加名称空间?

You can add the namespace to an element when you create the element. 您可以在创建元素时将名称空间添加到元素。 See XmlService.createElement(String, Namespace) . 请参见XmlService.createElement(String,Namespace) However, Google Apps Script will raise an error if you attempt to pass a string literal for the second argument. 但是,如果您尝试为第二个参数传递字符串文字,则Google Apps脚本将引发错误。 What you need to do is call XmlService.getNamespace(prefix, uri) method to create a Namespace object. 您需要做的是调用XmlService.getNamespace(prefix,uri)方法创建一个Namespace对象。 You can then pass that Namespace object as the second arg. 然后,您可以将该名称空间对象作为第二个arg传递。

// Create a namespace object.
ns = XmlService.getNamespace("xsi", 
    "http://www.w3.org/2001/XMLSchema-instance");

// Create an element.
var el = XmlService.createElement('devdef');

// Set the namespace of the element. 
el.setNamespace(ns);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 XmlService 的情况下解析 Google Apps 脚本中的 HTML 字符串? - How to parse an HTML string in Google Apps Script without using XmlService? 使用 Apps 脚本的 XmlService 发现带有“&amp;”符号的错误 - XmlService using Apps Script is finding an error with the '&' symbol 使用Google Apps脚本解析XML元素中的所有值? - Parse all values from a XML element using Google Apps Script? 如何使用Google Apps脚本在Google表单中获取网址参数? - How do you get URL parameters in a Google Form using Google Apps Script? Google Apps脚本:直接访问XML元素 - Google Apps Script: Directly accessing a XML element 如何将参数传递给Google Apps脚本调试器? - How do you pass an argument to the Google Apps Script debugger? 如果 REGEX 表达式的标准与使用 Google 应用程序脚本的单元格输入不匹配,您如何拒绝输入 - How do you reject input if criteria of REGEX expression is not a match to a cells input using Google apps script 如何使用Google Apps脚本为Google表单中的项目设置父项? - How to set the parent for item in a google form using google apps script? 如何在Google Apps脚本中设置类结构? - How do I set up a class structure in Google Apps Script? <a>使用Apps脚本向Google文档</a>添加<a>元素</a> - add an <a> element to Google Document using Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM