XSLT中添加复选框报错:受限命名空间不允许元素和属性
解决Word XSLT生成复选框的6102错误
错误原因
你遇到的6102错误是因为当前使用的是Word 2003旧版XML格式(w="http://schemas.microsoft.com/office/word/2003/wordml"),但代码中引入了Word 2010专属的w14命名空间元素(<w14:checkbox>)。MSXML处理器在验证旧版Word XML架构时,会将高版本命名空间标记为"受限",禁止使用这类元素。
解决方案:改用旧版Word支持的VML复选框
旧版Word XML不支持w14的结构化文档标签(SDT)复选框,需要替换为VML(矢量标记语言)实现的可交互复选框。以下是修改后的完整XSLT代码:
<?xml version="1.0" encoding="UTF-8"?> <?mso-application progid="Word.Document"?> <xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:v="urn:schemas-microsoft-com:vml"> <xslt:output method="xml" encoding="UTF-8" indent="yes"/> <xslt:template match="/"> <w:wordDocument xml:space="preserve"> <w:docPr> <w:view w:val="print"/> <w:zoom w:percent="130"/> <w:hideSpellingErrors w:val="on" /> <w:hideGrammaticalErrors w:val="on" /> <w:validateAgainstSchema w:val="off"/> <w:saveInvalidXML w:val="off"/> <w:ignoreMixedContent w:val="off"/> <w:doNotUnderlineInvalidXML w:val="on" /> <w:removeWordSchemaOnSave w:val="on" /> <w:useXSLTWhenSaving w:val="on" /> <w:saveThroughXSLT w:val="on" /> <w:showXMLTags w:val="off" /> <w:alwaysShowPlaceholderText w:val="off"/> <w:attachedTemplate w:val="Normal.dotm"/> </w:docPr> <w:body> <w:sectPr> <w:pgSz w:w="11906" w:h="16838" w:code="9"/> <w:pgMar w:top="567" w:right="1418" w:bottom="1134" w:left="1418" w:header="709" w:footer="709" w:gutter="0"/> </w:sectPr> <w:p/> <w:p> <w:r> <w:t>This is a checkbox:</w:t> </w:r> </w:p> <w:p/> <w:p/> <w:p> <!-- 可交互的VML复选框 --> <w:r> <w:rPr> <w:noProof/> </w:rPr> <v:shape id="_x0000_s1025" type="#_x0000_t201" style="width:18pt;height:18pt" o:formbutton="t"> <v:imagedata o:title=""/> <o:button type="checkbox" checked="f"/> </v:shape> </w:r> </w:p> </w:body> </w:wordDocument> </xslt:template> </xslt:stylesheet>
关键修改说明
- 移除了
w14和mc相关的命名空间及元素,避免触发架构验证错误。 - 使用
<v:shape>结合<o:button>创建复选框:o:formbutton="t"标记该形状为表单控件<o:button type="checkbox" checked="f"/>设置控件类型为复选框,初始状态为未选中(checked="t"则为选中)- 调整
style="width:18pt;height:18pt"可修改复选框大小
额外提示
如果需要基于XML数据动态控制复选框的选中状态,可以通过XSLT条件判断修改checked属性的值:
<o:button type="checkbox" checked="{xslt:if test="your-xml-node = 'true'"}t{xslt:else}f{xslt:if}"/>
内容的提问来源于stack exchange,提问作者Ella
相关产品推荐
相关产品推荐

