当我执行我的代码时,复制节点及其属性,而不是复制所有节点属性
例如 Microsoft Word ML 中的输入:-
<w:tblPr><w:tblW w:w="0" w:type="auto"/><w:tblBorders><w:top w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/></w:tblBorders><w:tblLook w:val="00BF"/></w:tblPr>
当我运行我的代码时,输出将是
<w:tblPr><w:tblW w:w="0"/><w:tblBorders><w:top w:val="single"/><w:left w:val="single"/><w:bottom w:val="single"/><w:right w:val="single"/><w:insideH w:val="single"/><w:insideV w:val="single"/></w:tblBorders><w:tblLook w:val="00BF"/></w:tblPr>
有一些缺少的属性没有被复制,当它们丢失时,我无法在 Microsoft Word 中打开文件。
如果有人可以帮助我,并指出我的代码有什么问题。或者我应该怎么做才能解决这个问题。我的代码如下
<?xml version="1.0" encoding="ISO-8859-1"?>
<?mso-application progid="Word.Document"?>
<xsl:stylesheet version="1.0" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:st1="urn:schemas-microsoft-com:office:smarttags">
<xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" media-type="application/html+xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>
<xsl:variable name="extNode" select="document('document4.xml') "/><!-- Document --><!--
Copy Element
Reprocesses Element in the output document:
Copies Attributes
Copies Text
Copies Child Nodes
--><!-- Copy Attribute -->
<xsl:template name="copy-attribute">
<xsl:param name="attribute"/>
<xsl:if test="$attribute">
<xsl:attribute name="{name($attribute)}">
<xsl:value-of select="$attribute"/>
</xsl:attribute>
</xsl:if>
</xsl:template>
<xsl:template match="*">
<xsl:variable name="Temp">
<xsl:if test="name()">
<xsl:value-of select="name()"/>
</xsl:if>
</xsl:variable>
<xsl:choose>
<xsl:when test="($Temp ='Place-Holder')">
<xsl:text>whatever text here simulating xml file</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:if test=".">
<xsl:element name="{name(.)}"><!-- Copy Attributes -->
<xsl:call-template name="copy-attribute">
<xsl:with-param name="attribute" select="./@*"/>
</xsl:call-template><!-- Copy Text -->
<xsl:value-of select="./text()"/>
<xsl:for-each select="*">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<w:wordDocument w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:st1="urn:schemas-microsoft-com:office:smarttags">
<xsl:for-each select="$extNode/w:wordDocument/child::node()"><!-- Microsoft word document design such as style,font,lists...etc -->
<xsl:if test=" name(.) != 'w:body'">
<xsl:copy-of select="."/>
</xsl:if><!-- MS document body where contents will be merged here-->
<xsl:if test="name(.) ='w:body'"><!-- MS document that contains contents,must have "Place-Holder" tag, so it will be known where to add contents-->
<w:body>
<xsl:for-each select="./child::node()">
<xsl:variable name="Temp">
<xsl:if test=".//Place-Holder">
<xsl:value-of select="position()"/>
</xsl:if>
</xsl:variable>
<xsl:if test="position() != $Temp">
<xsl:copy-of select="."/>
</xsl:if>
<xsl:if test="position() = $Temp">
<xsl:text disable-output-escaping="yes"/>
<xsl:for-each select="*">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</w:body>
</xsl:if>
</xsl:for-each>
</w:wordDocument>
</xsl:template>
</xsl:stylesheet>
我认为复制属性模板功能的问题。