首页 > 解决方案 > 使用 xml-fragment 进行 Xsl 转换

问题描述

我在下面有一个 xml 文件(切得不完整)。

<xml-fragment xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:c16r2="http://schemas.microsoft.com/office/drawing/2015/06/chart">
  <c:plotArea>
    <c:layout/>
    <c:barChart>
      <c:barDir val="col"/>
      <c:grouping val="clustered"/>
      <c:varyColors val="0"/>
      <c:ser>
        <c:idx val="0"/>
        <c:order val="0"/>
        <c:tx>
          <c:strRef>
            <c:f>Feuil1!$A$1</c:f>
            <c:strCache>
              <c:ptCount val="1"/>
              <c:pt idx="0">
                <c:v>hahah</c:v>

我想恢复 <c:v> 标记的值。但是这种语法是错误的:

...code...
<xsl:for-each select="c:val/c:numCache/c:pt">
    <td><xsl:value-of select="c:v"/></td>
</xsl:for-each>
...code...

我通过使用更简单的标签重新创建 xml 进行了测试,但同样无法解析,因为 xml-fragment 标签会导致所有错误

<xml-fragment xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:c16r2="http://schemas.microsoft.com/office/drawing/2015/06/chart">
    <stuff>
        <otherStuff>
            <collection>stuff1</collection>
        </otherStuff>
        <otherStuff>
            <collection>stuff2</collection>
        </otherStuff>
    </stuff>
</xml-fragment>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:safe="http://www.esa.int/safe/sentinel-1.0"
xmlns:bar="http://www.bar.org">
  <xsl:template match="/">
    <html>
    <body>
      <table border="1">
        <tr bgcolor="#9acd32">
          <th>Value 1</th>
          <th>Value 2</th>
        </tr>
        <xsl:for-each select="stuff/otherStuff">
            <td><xsl:value-of select="collection"/></td>
        </xsl:for-each>
      </table>
    </body>
  </html>
  </xsl:template>
</xsl:stylesheet>

我有两个问题:

提前致谢。

标签: xmlxslt

解决方案


对于您需要xsl:template match="/xml-fragment"xsl:for-each select="xml-fragment/stuff/otherStuff"


推荐阅读