使用 XSLT 生成 Microsoft 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>
我认为问题出在复制属性模板函数中。
When i execute my code, to copy nodes with their attributes, not, all of node attributes are copied
for example the input in 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>
whem i run my code, the output will be
<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>
There are some missing attributes which are not copied, when they are missed, i can't open the file in Microsoft Word.
IF someone could help me, and point out what is wrong with my code. or what i should do to solve this problem. My code as follows
<?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>
I think the problem in copy-attribute template function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了,我修改了这部分
问候,
I figured it out, i modified this part
Regards,