【问题标题】:XSLT Can't Read an Excel XML File?XSLT 无法读取 Excel XML 文件?
【发布时间】:2021-12-27 20:22:18
【问题描述】:

我正在使用 XSLT / XPath 浏览您在解压缩 Excel 文件时获得的一些 XML 文件。我发现了一个我似乎无法阅读的“关系”文件 workbook.xml.rels,使用的代码类似于成功读取 workbook.xml 的代码文件。

这里是一些 workbook.xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
    ...
    <sheets>
        <sheet name="Sheet1"
               sheetId="2"
               r:id="rId1"/>
        <sheet name="Test Sheet"
               sheetId="1"
               r:id="rId2"/>
    </sheets>
    ...
</workbook>

这是 workbook.xml.rels 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId3"
             Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"
                  Target="theme/theme1.xml"/>
    <Relationship Id="rId2"
                  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
                  Target="worksheets/sheet2.xml"/>
    <Relationship Id="rId1"
                  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
                  Target="worksheets/sheet1.xml"/>
    <Relationship Id="rId5"
                  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
                  Target="sharedStrings.xml"/>
    <Relationship Id="rId4"
                  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
                  Target="styles.xml"/>
</Relationships>

这是一些 XSLT:

<?xml version="1.0"?>
<!-- greeting.xsl -->
<xsl:stylesheet
    ... 
    <xsl:output method="text"/>
    <xsl:variable name="baseDir"       select="replace(document-uri(.), '(.*[\\/]xl).*', '$1/')"/>
    <xsl:variable name="workbook"      select="concat($baseDir, 'workbook.xml')"/>
    <xsl:variable name="theSheetId" select="doc($workbook)/workbook/sheets/sheet[matches(@name, 'Test Sheet')]/@r:id"/>
    <xsl:variable name="workbook_rels" select="concat($baseDir, '_rels/workbook.xml.rels')"/>

    <!-- code to read workbook.xml.rels -->
    <xsl:variable name="theSheet"     select="doc($workbook_rels)/Relationships/Relationship[matches(@Id, $theSheetId)]/@Target"/>

    <xsl:template match="/">
        <xsl:text>&#x0A;baseDir       = </xsl:text><xsl:value-of select="$baseDir"/>
        <xsl:text>&#x0A;workbook      = </xsl:text><xsl:value-of select="$workbook"/>
        <xsl:text>&#x0A;workbook_rels = </xsl:text><xsl:value-of select="$workbook_rels"/>
        <xsl:text>&#x0A;theSheetId    = </xsl:text><xsl:value-of select="$theSheetId"/>
        <xsl:text>&#x0A;theSheet      = </xsl:text><xsl:value-of select="$theSheet"/>
        <xsl:text>&#x0A;end</xsl:text>
    </xsl:template>
</xsl:stylesheet>

还有输出:

baseDir         = file:/C:/Training/sandbox/conv_/xl/
workbook        = file:/C:/Training/sandbox/conv_/xl/workbook.xml
workbook_rels   = file:/C:/Training/sandbox/conv_/xl/_rels/workbook.xml.rels
theSheetId      = rId2
theSheet        = **<I get nothing here>**
end

您可以看到在读取 workbook.xml 时正确设置了“theSheetID”变量。但是,当我使用该变量将相应的目标值从 workbook.xml.rels 获取到“theSheet”变量时,我什么也得不到。我尝试用一​​个数字替换匹配表达式,但我仍然一无所获。读取此类文件有问题吗?

建议?谢谢!

【问题讨论】:

  • 请发布一个可重现的示例,而不是脱离上下文的代码的 sn-ps。

标签: excel xml xslt


【解决方案1】:

matchesreplace 的使用表明您正在使用 XSLT 2 或 3 处理器,这样您当然可以声明 xpath-default-namespace 的 XSLT 2 或 3,您只需要了解您必须在处理来自不同命名空间的元素的部分,例如&lt;xsl:variable name="theSheet" select="doc($workbook_rels)/Relationships/Relationship[matches(@Id, $theSheetId)]/@Target" xpath-default-namespace="http://schemas.openxmlformats.org/package/2006/relationships"/&gt;.

鉴于示例,我宁愿使用键 &lt;xsl:key name="rel" match="Relationships/Relationship" use="@Id" xpath-default-namespace="http://schemas.openxmlformats.org/package/2006/relationships"/&gt;,然后使用 &lt;xsl:variable name="theSheet" select="key('rel,$theSheetId, doc($workbook_rels))/@Target"/&gt;,但在从特定文档中选择元素时使用 xpath-default-namespace 声明相关命名空间可能是您的 XSLT 中缺少的内容。

【讨论】:

  • 成功了,谢谢!我怀疑它与命名空间有关,但在我的新手级别,我不知道如何解决它。我也会学习钥匙的使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 2017-03-21
  • 1970-01-01
相关资源
最近更新 更多