问题
I find a appearance , that I can't get a correct result with fn:key function in a template which has a name attribute. When I just delete the name attribute of xsl:template, the result are right. That amazes me. Would somebody like to tell the reason? thanks.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:map = "http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="xs w rel r map"
version="2.0">
<xsl:key name="key_styles_id" match="w:style" use="@w:styleId"/>
<xsl:template match="/">
<xsl:variable name="styles" select="doc(resolve-uri('styles.xml', document-uri(/)))" as="document-node()?"/>
<xsl:apply-templates select="w:document/w:body/w:p">
<xsl:with-param name="styles" tunnel="yes" select="$styles"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template name="all_paragraph" match="w:p">
<xsl:param name="styles" tunnel="yes" required="yes" />
<xsl:value-of select="$styles"/>
<xsl:variable name="style_id" select="w:pPr/w:pStyle/@w:val" as="xs:string?"/>
<xsl:variable name="style_node" select="key('key_styles_id', $style_id, $styles)"/>
<xsl:value-of select="$style_node"/>
</xsl:template>
</xsl:stylesheet>
The input xml is the documment.xml in directory of a unzip docx, the docx file can be replaced to a file that have some words with styles.
The problem is that: If I delete the name attribute whose value is 'all_paragraph', the key function will get the right result and $style_node has many elements data. if not , the key function return empty and $style_node has nothing.
The xslt processor that I use is saxon-he 9.8.0.12
input source: document.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr>
<w:b/>
<w:b/>
<w:bCs/>
<w:color w:val="C9211E"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
<w:bCs/>
<w:color w:val="C9211E"/>
</w:rPr>
<w:t xml:space="preserve">12345good day. </w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr>
<w:i/>
<w:i/>
<w:iCs/>
<w:color w:val="77BC65"/>
<w:sz w:val="28"/>
<w:szCs w:val="28"/>
<w:highlight w:val="yellow"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:i/>
<w:iCs/>
<w:color w:val="77BC65"/>
<w:sz w:val="28"/>
<w:szCs w:val="28"/>
<w:highlight w:val="yellow"/>
</w:rPr>
<w:t>64789hello world</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:type w:val="nextPage"/>
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:left="1134" w:right="1134" w:header="0" w:top="1134" w:footer="0"
w:bottom="1134" w:gutter="0"/>
<w:pgNumType w:fmt="decimal"/>
<w:formProt w:val="false"/>
<w:textDirection w:val="lrTb"/>
</w:sectPr>
</w:body>
</w:document>
styles.xml which used in xslt code like doc('styles.xml')
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14">
<w:docDefaults>
<w:rPrDefault>
<w:rPr>
<w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="新宋体"
w:cs="Lucida Sans"/>
<w:kern w:val="2"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
<w:lang w:val="en-US" w:eastAsia="zh-CN" w:bidi="hi-IN"/>
</w:rPr>
</w:rPrDefault>
<w:pPrDefault>
<w:pPr>
<w:widowControl/>
</w:pPr>
</w:pPrDefault>
</w:docDefaults>
<w:style w:type="paragraph" w:styleId="Normal">
<w:name w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:widowControl/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Liberation Serif" w:hAnsi="Liberation Serif" w:eastAsia="新宋体"
w:cs="Lucida Sans"/>
<w:color w:val="auto"/>
<w:kern w:val="2"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
<w:lang w:val="en-US" w:eastAsia="zh-CN" w:bidi="hi-IN"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style14">
<w:name w:val="标题样式"/>
<w:basedOn w:val="Normal"/>
<w:next w:val="Style15"/>
<w:qFormat/>
<w:pPr>
<w:keepNext w:val="true"/>
<w:spacing w:before="240" w:after="120"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Liberation Sans" w:hAnsi="Liberation Sans" w:eastAsia="微软雅黑"
w:cs="Lucida Sans"/>
<w:sz w:val="28"/>
<w:szCs w:val="28"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style15">
<w:name w:val="Body Text"/>
<w:basedOn w:val="Normal"/>
<w:pPr>
<w:spacing w:lineRule="auto" w:line="276" w:before="0" w:after="140"/>
</w:pPr>
<w:rPr/>
</w:style>
<w:style w:type="paragraph" w:styleId="Style16">
<w:name w:val="List"/>
<w:basedOn w:val="Style15"/>
<w:pPr/>
<w:rPr>
<w:rFonts w:cs="Lucida Sans"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style17">
<w:name w:val="Caption"/>
<w:basedOn w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:suppressLineNumbers/>
<w:spacing w:before="120" w:after="120"/>
</w:pPr>
<w:rPr>
<w:rFonts w:cs="Lucida Sans"/>
<w:i/>
<w:iCs/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
</w:style>
<w:style w:type="paragraph" w:styleId="Style18">
<w:name w:val="索引"/>
<w:basedOn w:val="Normal"/>
<w:qFormat/>
<w:pPr>
<w:suppressLineNumbers/>
</w:pPr>
<w:rPr>
<w:rFonts w:cs="Lucida Sans"/>
</w:rPr>
</w:style>
</w:styles>
my screenshots:
first result with name attribute
2nd result without name attribute
来源:https://stackoverflow.com/questions/59151172/does-the-name-of-the-xsltemplate-effects-the-result-of-function-fnkey-and-why