XSLT ignores elements in template?
I have the following XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:o=开发者_如何转开发"urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="ss:Workbook/o:DocumentProperties/o:*"/>
<xsl:template match="ss:Workbook/x:ExcelWorkbook/x:*"/>
<xsl:template match="ss:Workbook/x:ExcelWorkbook/x:*"/>
<xsl:template match="ss:Workbook/ss:Worksheet/x:WorksheetOptions/x:*"/>
<xsl:template match="ss:Workbook/ss:DocumentProperties/ss:*"/>
<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
<grade-dist>
<xsl:apply-templates select='ss:Workbook/ss:Worksheet/ss:Table'/>
</grade-dist>
</xsl:template>
<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
....
My XML outputs fine, but I dont have the: <grade-dist>
and </grade-dist>
in it, seems like it ignores both completely, any idea why?
Thanks,
You have two templates with exactly the same match pattern: 'ss:Workbook/ss:Worksheet/ss:Table'
According to the XSLT spec this is a recoverable error and the recovery observed here is that the template that comes last get chosen.
Another observation is that the instruction:
<xsl:apply-templates select='ss:Workbook/ss:Worksheet/ss:Table'/>
inside the template matching 'ss:Workbook/ss:Worksheet/ss:Table'
, is most probably wrong -- it is unlikely that there would be 'ss:Workbook/ss:Worksheet/ss:Table'
elements that have a 'ss:Workbook/ss:Worksheet/ss:Table'
grand-grand-parent.
I think what you want is something like this:
<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
<grade-dist>
<xsl:apply-templates select="." mode="pr2"/>
</grade-dist>
</xsl:template>
<xsl:template mode="pr2" match='ss:Workbook/ss:Worksheet/ss:Table'>
<!-- Some necessary processing -->
</xsl:template>
or just:
<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
<grade-dist>
<!-- Do the processing here -->
</grade-dist>
</xsl:template>
It looks as if you have two templates whose match criteria are identical (i.e., no mode or priority attributes to differentiate them). If the processor is bypassing the first one and processing the second because it "wins," then this would have the effect you are observing (since I presume the purpose of the apply-templates inside the grade-dist is to invoke the second template).
精彩评论