gpt4 book ai didi

javascript - 如何在 JavaScript 中使用 XPath 从命名空间的 OOXML 中进行选择?

转载 作者:行者123 更新时间:2023-11-30 10:57:48 25 4
gpt4 key购买 nike

我正在构建一个工具,以使用 XPath 表达式从用户指定的 XML 文件中获取数据。简单的 XML 文件和具有单个 namespace 的 XML 文件工作正常,但我没有让以下 XML 文件工作,其中相同的前缀在不同的层次级别被覆盖:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Microsoft Office User</Author>
<LastAuthor>Microsoft Office User</LastAuthor>
<Created>2019-12-03T15:40:13Z</Created>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>13820</WindowHeight>
<WindowWidth>23740</WindowWidth>
<WindowTopX>1520</WindowTopX>
<WindowTopY>1620</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
</Styles>
<Worksheet ss:Name="Blad1">
<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="8" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="65" ss:DefaultRowHeight="16">
<Row>
<Cell><Data ss:Type="String">dfgdfgdfg</Data></Cell>
<Cell><Data ss:Type="Number">1150</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">werwerwe</Data></Cell>
<Cell><Data ss:Type="Number">889</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<RangeSelection>R1C1:R8C2</RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>

我正在测试的xpath是

/Workbook[@xmlns="urn:schemas-microsoft-com:office:spreadsheet"]/Worksheet[@ss:Name="Blad1"]/Table[@ss:ExpandedColumnCount="2"]/Row[1]/Cell[2]/Data[@ss:Type="Number"]/text()

这是解析和搜索节点的代码:

// downloadedData is a string with the xml, xPath is the string with the xpath

var parser, xmlDoc;

parser = new DOMParser();
xmlDoc = parser.parseFromString(downloadedData, "text/xml");

var xmlEvaluator = new XPathEvaluator();
var xmlResolver = xmlEvaluator.createNSResolver(xmlDoc);
var node = xmlEvaluator.evaluate(xPath, xmlDoc, xmlResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

/// node always empty here

编辑: 删除了代码中的拼写错误(与实际问题无关)

最佳答案

XPath 数据模型中的命名空间不是属性,所以这永远行不通:

Workbook[@xmlns="urn:schemas-microsoft-com:office:spreadsheet"]

相反,您需要绑定(bind)命名空间前缀并在您的元素名称中使用它们:

x:Workbook

其中命名空间前缀 x 绑定(bind)到 URI“urn:schemas-microsoft-com:office:spreadsheet”。绑定(bind)是使用 namespaceResolver 建立的,如下所述:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_using_XPath_in_JavaScript

关于javascript - 如何在 JavaScript 中使用 XPath 从命名空间的 OOXML 中进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59308058/

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