使用XSLT生成Microsoft Word ML时输出问题

时间:2011-05-18 21:04:58

标签: xml xslt ms-word ml

当我执行我的代码时,要复制带有属性的节点,而不是复制所有节点属性

例如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>

我认为复制属性模板功能存在问题。

1 个答案:

答案 0 :(得分:0)

我想通了,我修改了这部分

    <xsl:template name="copy-attribute">
    <xsl:param name="attribute"/>
    <xsl:if test="$attribute">  
        <xsl:for-each select="@*">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>

         </xsl:attribute>
        </xsl:for-each>
    </xsl:if>
</xsl:template>

此致