如何从带有命名空间的 XML 中“选择"? [英] How to 'select' from XML with namespaces?
本文介绍了如何从带有命名空间的 XML 中“选择"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似于 :::
I have an XML document something like :::
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="Worksheet1">
<Table>
<Column ss:Width="100"></Column>
<Row>
<Cell ss:Index="1" ss:StyleID="headerStyle">
<Data ss:Type="String">Submitted By</Data>
</Cell>
</Row>
<Row>
<Cell ss:Index="1" ss:StyleID="alternatingItemStyle">
<Data ss:Type="String">Value1-0</Data>
</Cell>
</Row>
</Table>
<AutoFilter xmlns="urn:schemas-microsoft-com:office:excel"
x:Range="R1C1:R1C5"></AutoFilter>
</Worksheet>
</Workbook>
问题是在尝试使用
<xsl:for-each select="//Row">
<xsl:copy-of select="."/>
</xsl:for-each>
它不匹配.我删除了所有的名称间距,它工作正常.那么,如何让选择"与行匹配?
It is not matching. I removed all of the name-spacing and it works fine. So, how do I get the 'select' to match Row?
推荐答案
为 XSLT 中的命名空间声明一个命名空间前缀,然后使用该前缀select
:
Declare a namespace prefix for the namespace in your XSLT and then select
using that prefix:
<xsl:stylesheet ... xmlns:os="urn:schemas-microsoft-com:office:spreadsheet">
...
<xsl:for-each select="//os:Row">
...
</xsl:for-each>
...
</xsl:stylesheet>
这通常会导致 XPath 易于阅读.但是,XSLT/XPath 工具会生成以下等效代码:
This usually results in XPaths that are easy to read. However, XSLT/XPath tools generate the following, equivalent code:
<xsl:for-each select="//*[local-name()='Row' = and namespace-uri()='urn:schemas-microsoft-com:office:spreadsheet']">
...
</xsl:for-each>
这篇关于如何从带有命名空间的 XML 中“选择"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文