XSLT超出表达式末尾的意外令牌

时间:2019-07-04 03:38:38

标签: excel xml xslt ms-word

这是从Excel文档转换而来的源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>Passiflora Cui</Author>
  <LastAuthor>Passiflora Cui</LastAuthor>
  <Created>2019-06-30T21:49:41Z</Created>
  <LastSaved>2019-06-30T21:50:54Z</LastSaved>
  <Version>16.00</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>16940</WindowHeight>
  <WindowWidth>27640</WindowWidth>
  <WindowTopX>5580</WindowTopX>
  <WindowTopY>3560</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>
  <Style ss:ID="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="5" x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="65" ss:DefaultRowHeight="16">
   <Column ss:AutoFitWidth="0" ss:Width="74"/>
   <Row>
    <Cell><Data ss:Type="String">Field1</Data></Cell>
    <Cell><Data ss:Type="String">Field2</Data></Cell>
    <Cell><Data ss:Type="String">Field3</Data></Cell>
    <Cell><Data ss:Type="String">Field4</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">Field1_Data1</Data></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">Field2_Data1</Data></Cell>
    <Cell><Data ss:Type="String">Field3_Data1</Data></Cell>
    <Cell><Data ss:Type="String">Field4_Data1</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">Field1_Data2</Data></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">Field2_Data2</Data></Cell>
    <Cell><Data ss:Type="String">Field3_Data2</Data></Cell>
    <Cell><Data ss:Type="String">Field4_Data2</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">Field1_Data3</Data></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">Field2_Data3</Data></Cell>
    <Cell><Data ss:Type="String">Field3_Data3</Data></Cell>
    <Cell><Data ss:Type="String">Field4_Data3</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">Field1_Data4</Data></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">Field2_Data4</Data></Cell>
    <Cell><Data ss:Type="String">Field3_Data4</Data></Cell>
    <Cell><Data ss:Type="String">Field4_Data4</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>
     <ActiveRow>5</ActiveRow>
     <ActiveCol>5</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>

这是我的XSLT,它从较大的部分中提取了问题部分,该部分将Excel XML文件转换为Word XML文件,其中<a:ext></a:ext>元素引起了错误:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:wb="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
    <xsl:output method = "xml" indent = "yes"/>

    <!-- Ignore all free text() nodes -->
    <xsl:template match="text()" />

    <xsl:template match = "/wb:Workbook/wb:Worksheet/wb:Table">
        <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
            <Result>
                <xsl:for-each select="wb:Row[position() > 1]">
                    <Test>
                        <Field1><xsl:value-of select = "wb:Cell[1]/wb:Data"/></Field1>
                        <Field2><xsl:value-of select = "wb:Cell[2]/wb:Data"/></Field2>
                        <Field3><xsl:value-of select = "wb:Cell[3]/wb:Data"/></Field3>
                        <Field4><xsl:value-of select = "wb:Cell[4]/wb:Data"/></Field4>
                    </Test>
                </xsl:for-each>
            </Result>
            <!--The following a:ext element is just directly copied to the result, which causes the errors-->
            <a:ext uri="{05A4C25C-085E-4340-85A3-A5531E510DB2}">
                <thm15:themeFamily xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main" name="Office Theme" id="{62F939B6-93AF-4DB8-9C6B-D6C7DFDC589F}" vid="{4A3C46E8-61CC-4603-A589-7422A47A8E4A}"/>
            </a:ext>
        </pkg:package>
    </xsl:template>
</xsl:stylesheet>

<a:ext></a:ext>元素是从Word XML复制的,只想直接复制到结果中。但是,它报告错误“超出表达式末尾的意外令牌<name>”,完整的错误消息如下:

Severity: warning
Description: Error in expression 05A4C25C-085E-4340-85A3-A5531E510DB2: Unexpected token <name> beyond end of expression
Start location: 22:0

Severity: warning
Description: Error in expression 62F939B6-93AF-4DB8-9C6B-D6C7DFDC589F: Unexpected token <name> beyond end of expression
Start location: 23:0

Severity: warning
Description: Error in expression 4A3C46E8-61CC-4603-A589-7422A47A8E4A: Unexpected token <name> beyond end of expression
Start location: 23:0

如何解决此问题?预先感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

属性中的大括号表示将内容视为要评估的XPATH表达式。要获得所需的内容,您需要加倍括号以使其逃脱:

        <a:ext uri="{{05A4C25C-085E-4340-85A3-A5531E510DB2}}">
            <thm15:themeFamily 
                xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main" 
                name="Office Theme" 
                id="{{62F939B6-93AF-4DB8-9C6B-D6C7DFDC589F}}" 
                vid="{{4A3C46E8-61CC-4603-A589-7422A47A8E4A}}"/>
        </a:ext>