[英]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.