我想通过使用field设置锁定来防止Word文档字段更新w:fldLock
。我有一个xml
包含该节点的文件w:fldSimple
。每当我找到该节点时,我都想将属性w:fldLock
设置为此节点。为此,我想使用XSLT转换。您能告诉我样本XSL转换吗?
示例xml数据:
<?xml version="1.0" encoding="utf-8" ?>
<w:hdr mc:Ignorable="w14 wp14" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<w:p w:rsidR="00235C27" w:rsidRDefault="00235C27">
<w:pPr>
<w:pStyle w:val="Header" />
</w:pPr>
<w:fldSimple w:instr="MERGEFIELD firstname \* MERGEFORMAT">
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>John</w:t>
</w:r>
</w:fldSimple>
</w:p>
</w:hdr>
XSLT之后,我的输出应为如下所示的xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<w:hdr mc:Ignorable="w14 wp14" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<w:p w:rsidR="00235C27" w:rsidRDefault="00235C27">
<w:pPr>
<w:pStyle w:val="Header" />
</w:pPr>
<w:fldSimple w:instr="MERGEFIELD firstname \* MERGEFORMAT" w:fldLock = "1">
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>John</w:t>
</w:r>
</w:fldSimple>
</w:p>
</w:hdr>
请给我建议吗?
以下应该可以解决问题。它使用标识转换复制所有元素,然后处理w:fldSimple
<?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"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Modify the identity transform to slip in the extra attribute -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:if test="name()='w:fldSimple'">
<xsl:attribute name="w:fldLock">1</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
(首先添加属性,但这无关紧要)
<w:fldSimple w:fldLock="1" w:instr="MERGEFIELD firstname \* MERGEFORMAT">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句