将新的 xml 属性插入 MS Word xml 文档元素



我想通过使用字段w:fldLock设置锁定来防止 Word 文档字段更新。我有一个包含节点w:fldSimplexml文件。每当我找到这个节点时,我都想设置这个节点的属性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">

请让我知道这段代码是否可行?@StuartLc 因为我不知道身份转换,请让我知道你给出的代码安全吗?

<?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:strip-space elements="*"/>
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:choose>
      <xsl:when test="starts-with(name(),'w:fldSimple')">
        <xsl:element name="{name()}">
          <xsl:attribute name="w:fldLock">
           1
        </xsl:attribute>
          <xsl:apply-templates select="node()"/>
        </xsl:element>
      </xsl:when>
      <xsl:when test="name()=''">
        <xsl:value-of select="."/>
        <xsl:copy-of select="@*"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

最新更新