如何使用简单的样式表转换 ms excel xml?

2024-01-12

我需要使用样式表转换以 xml 格式保存的 Excel 电子表格...但我一直对 ms 使用的许多命名空间感到困惑。我需要一个XSLT 样式表允许输入 xml 的每个元素传递(当我使用样式表时,默认值似乎传递所有文本,否则该样式表应该只传递我正在编写的元素模板)。

这是输入 xml:

http://apriority.dyndns.biz/im_cache/u_1/j_2390/2_9/tf/testexcel.xml http://apriority.dyndns.biz/im_cache/u_1/j_2390/2_9/tf/testexcel.xml

:我需要简单地从列中的两张表中提取数据,例如:

Output:

Sheet 1
1) blah, blue , burn, baste, belly, belie, bestow, betrothed, bemoan
2) quack, quagmire, quick, quantum, quant
3) alimony, ashy, amber, absolute, astrology, alabaster, angry
4) cost, curry, candor, cabin, capability, castor, canada

Sheet 2
1) 3
2) 32
3) 322
....etc.

我在样式表方面遇到了麻烦...与 MS 在输入 xml 中使用的命名空间有关...即使我只有一个元素模板(比如提取仅第一张纸的行)...什么样式表代码将获得上面的输出???

这是我到目前为止的样式表:

http://apriority.dyndns.biz/im_cache/u_1/j_2390/2_9/tf/learningmap.xsl http://apriority.dyndns.biz/im_cache/u_1/j_2390/2_9/tf/learningmap.xsl

文件放在非 https 网址上,因此没有病毒风险另外,我使用的是我的真实姓名,请用谷歌搜索我!

这是使用 Mads 建议的代码后编辑的样式表!

http://apriority.dyndns.biz/im_cache/u_1/j_2390/2_9/tf/learningmap_mod.xsl http://apriority.dyndns.biz/im_cache/u_1/j_2390/2_9/tf/learningmap_mod.xsl

我仍然有一个问题,即后续代码中没有提取实际的文本数据,为什么我无法获取文本数据。我可以输出第一个变量“snid”,但所有文本变量都不会出现在输出中,即使我选择它们并且原始源 xml 在这些条目中包含内容。对于这个新问题的任何帮助将不胜感激!

2 月 9 日更新:

我解决了变量映射失败的问题。这是一个简单的 xpath 错误,我正在寻址不存在的节点。单元格与其行之间存在一对一的映射,因此变量应提取为 Cell[k].Row[1]...Cell[k+1].Row[1]...等。

输出转换按照我的需要进行,感谢您对答案的贡献。这个很难判断哪个答案被接受,因为两个提交的内容都有帮助,但这次我必须把它交给 Mads Hanson。谢谢!


  • Namespaces are inherited from the parents, and the <Worksheet> element uses it without a namespace prefix, so it may not have been apparent that Worksheet, Data, Row, and Cell were all bound to the same namespace and needed ss: for each of the match criteria.
    • 您已正确声明urn:schemas-microsoft-com:office:spreadsheet名称空间,但没有使用它来匹配所有内容。
  • 在模板中ss:Worksheet您没有应用模板,因此处理正在停止。我添加了一个apply-templates for ss:Table/ss:Row
  • It looked as if you were creating text output
    • 我改变了输出method="xml" to method="text"
    • 我更换了<br/> with &#xA;(换行符)

我已经修改了您的样式表以生成您所显示的输出。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
         xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >
    <xsl:output method="text"/>

    <xsl:template match="ss:Workbook">
        <xsl:apply-templates select="ss:Worksheet"/>
    </xsl:template>

    <xsl:template match="ss:Worksheet">
        <xsl:value-of select="@ss:Name"/>
        <xsl:text>:&#xA;</xsl:text>
        <xsl:apply-templates select="ss:Table/ss:Row" />
    </xsl:template>

    <xsl:template match="ss:Row">
        <xsl:apply-templates select="ss:Cell"/>
    </xsl:template>

    <xsl:template match="ss:Cell">
        <xsl:apply-templates select="ss:Data"/>
    </xsl:template>

    <xsl:template match="ss:Data">
        <xsl:value-of select="count(../preceding-sibling::ss:Cell) + 1"/>
        <xsl:text>)</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用简单的样式表转换 ms excel xml? 的相关文章

随机推荐