Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中使用XPath从名称空间的OOXML中进行选择?_Javascript_Xml_Xpath_Openxml_Spreadsheetml - Fatal编程技术网

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

如何在JavaScript中使用XPath从名称空间的OOXML中进行选择?,javascript,xml,xpath,openxml,spreadsheetml,Javascript,Xml,Xpath,Openxml,Spreadsheetml,我正在构建一个工具,用XPath表达式从用户指定的XML文件中获取数据。简单的XML文件和具有单个名称空间的XML文件可以正常工作,但如果在不同的层次级别覆盖相同的前缀,则无法使用以下XML文件: <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="

我正在构建一个工具,用XPath表达式从用户指定的XML文件中获取数据。简单的XML文件和具有单个名称空间的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>
这是解析和搜索节点的代码:

// 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"]
相反,您需要绑定命名空间前缀并在元素名称中使用它们:

x:Workbook
其中名称空间前缀x绑定到URI“urn:schemas microsoft com:office:spreadsheet”。绑定是使用名称空间解析器建立的,如下所述:


XPath数据模型中的名称空间不是属性,因此这永远不会起作用:

Workbook[@xmlns="urn:schemas-microsoft-com:office:spreadsheet"]
相反,您需要绑定命名空间前缀并在元素名称中使用它们:

x:Workbook
其中名称空间前缀x绑定到URI“urn:schemas microsoft com:office:spreadsheet”。绑定是使用名称空间解析器建立的,如下所述:


@MichaelKay正确地指出名称空间不是属性。下面是一些支持细节,用于进行必要的调整,以便XPath能够正常工作

  • 修复一个无关的bug。改变

    xmlDoc = parser.parseFromString(downloadedData, ''), "text/xml");
    
    这在语法上是不正确的

    xmlDoc = parser.parseFromString(downloadedData, 'text/xml');
    
  • 请注意,您的XML已经为所有需要的组件声明了名称空间前缀,因此您不必创建自定义的
    nsResolver()
    ;按您所做的方式调用
    xmlEvaluator.createNSResolver()
    就足够了

  • 修复XPath以正确使用命名空间前缀:

    xPath = '/ss:Workbook/ss:Worksheet[@ss:Name="Blad1"]/ss:Table[@ss:ExpandedColumnCount="2"]/ss:Row[1]/ss:Cell[2]/ss:Data[@ss:Type="Number"]/text()';
    
  • 您的代码现在可以正常运行,允许针对OOXML选择XPath


    另请参见

    @MichaelKay正确地指出名称空间不是属性。下面是一些支持细节,用于进行必要的调整,以便XPath能够正常工作

  • 修复一个无关的bug。改变

    xmlDoc = parser.parseFromString(downloadedData, ''), "text/xml");
    
    这在语法上是不正确的

    xmlDoc = parser.parseFromString(downloadedData, 'text/xml');
    
  • 请注意,您的XML已经为所有需要的组件声明了名称空间前缀,因此您不必创建自定义的
    nsResolver()
    ;按您所做的方式调用
    xmlEvaluator.createNSResolver()
    就足够了

  • 修复XPath以正确使用命名空间前缀:

    xPath = '/ss:Workbook/ss:Worksheet[@ss:Name="Blad1"]/ss:Table[@ss:ExpandedColumnCount="2"]/ss:Row[1]/ss:Cell[2]/ss:Data[@ss:Type="Number"]/text()';
    
  • 您的代码现在可以正常运行,允许针对OOXML选择XPath


    另请参见

    第3条解决了它,正如你们都提到的,我在xpath中的名称空间有问题,感谢一个工作示例。第3条解决了它,正如你们都提到的,我在xpath中的名称空间有问题,感谢一个工作示例