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

时间:2014-01-10 10:19:11

标签: xml xslt

我想通过使用字段w:fldLock设置锁定来防止word文档字段更新。我有一个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>

请告诉我这个?

2 个答案:

答案 0 :(得分:0)

以下应该可以解决问题。它使用身份转换复制所有元素,然后处理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">

答案 1 :(得分:0)

请告诉我这段代码是否可行? @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>