gpt4 book ai didi

javascript - 在 Node.js 中使用 xmldom.XMLSerializer 获取小写标签名称

转载 作者:搜寻专家 更新时间:2023-10-31 23:55:08 27 4
gpt4 key购买 nike

我正在使用 Node.js 将 D3 图形保存到文件中。代码将 SVG 标签返回为全部大写而不是小写标签:

<SVG height="300" width="460" xmlns="http://www.w3.org/2000/svg">
<G transform="translate(230,150)">
<PATH d="M6.123031769111886e-15,-100A100,100 0 0,1 90.12430862004372,43.33138580473872L45.06215431002186,21.66569290236936A50,50 0 0,0 3.061515884555943e-15,-50Z" fill="#1f77b4"/>
<PATH d="M90.12430862004372,43.33138580473872A100,100 0 0,1 4.27399232527422,99.90862319941907L2.13699616263711,49.954311599709534A50,50 0 0,0 45.06215431002186,21.66569290236936Z" fill="#aec7e8"/>
<PATH d="M4.27399232527422,99.90862319941907A100,100 0 0,1 -64.73671010012329,76.21783495621347L-32.368355050061645,38.10891747810673A50,50 0 0,0 2.13699616263711,49.954311599709534Z" fill="#ff7f0e"/>
<PATH d="M-64.73671010012329,76.21783495621347A100,100 0 0,1 -99.89978970073908,-4.4757142165364L-49.94989485036954,-2.2378571082682A50,50 0 0,0 -32.368355050061645,38.10891747810673Z" fill="#ffbb78"/>
<PATH d="M-99.89978970073908,-4.4757142165364A100,100 0 0,1 -1.836909530733566e-14,-100L-9.18454765366783e-15,-50A50,50 0 0,0 -49.94989485036954,-2.2378571082682Z" fill="#2ca02c"/>
</G>
</SVG>

node.js 脚本:

require('d3');
var xmldom = require('xmldom');

var dataset = {
apples: [53245, 28479, 19697, 24037, 40245],
};

var width = 460,
height = 300,
radius = Math.min(width, height) / 2;

var color = d3.scale.category20();

var pie = d3.layout.pie()
.sort(null);

var arc = d3.svg.arc()
.innerRadius(radius - 100)
.outerRadius(radius - 50);

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var path = svg.selectAll("path")
.data(pie(dataset.apples))
.enter().append("path")
.attr("fill", function (d, i) {
return color(i);
})
.attr("d", arc);
var svgObject = d3.select("svg").attr('xmlns', 'http://www.w3.org/2000/svg');
var xml = new xmldom.XMLSerializer().serializeToString(svgObject[0][0]);

console.log(xml);

如何将标签名称作为小写名称(SVG 需要)?

最佳答案

不幸的是,xmldom 目前似乎不支持此小写选项。

所以你可以ask for it on github或者同时 fork 你自己的模块版本。

您必须更改此行以获取标签名称的小写版本:

https://github.com/jindw/xmldom/blob/master/dom.js#L919

使用

var nodeName = node.localName;

代替

var nodeName = node.tagName;

关于javascript - 在 Node.js 中使用 xmldom.XMLSerializer 获取小写标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com