我已经住在这些XSL“世界”中了几个星期了,我对所谓的‘WordML处理器’处理事情的方式感到非常不满。
对于一个老问题,其目的是(如果-we仍然可以称之为简单的话)将Light文件转换为格式良好的WordML文件。
对这个问题我很抱歉,但我想没有其他解释的方法了。
我有以下XML文档
<?xml version="1.0" encoding="utf-8" ?>
<body>
<heading>
This is the <bold><italic>standard</italic> text</bold> run.
</heading>
<copyright/>
</body>其目的是按照WordML文档分别格式化每个段落和字符样式:
因此,预期的WordML文档输出如下:
<w:wordDocument xmlns="http://www.w3.org/1999/xhtml"
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xml:space="preserve">
<w:body>
<w:p>
<w:pPr><w:pStyle w:val="Heading"/></w:pPr>
<w:r>
<w:t>This is the </w:t>
<w:rPr><w:b w:val="on"/></w:rPr>
<w:rPr><w:i w:val="on"/></w:rPr>
<w:t>standard </w:t>
<w:rPr><w:i w:val="off"/></w:rPr>
<w:t>text </w:t>
<w:rPr><w:b w:val="off"/></w:rPr>
<w:t>run.</w:t>
</w:r>
</w:p>
</w:body>
</w:wordDocument>使用以下XSL模板文件(将由您的反馈进行更正):
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xml:space="preserve">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="body">
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns="http://www.w3.org/1999/xhtml"
xml:space="preserve">
<w:body>
<xsl:apply-templates match="normal|heading"/>
</w:body>
</w:wordDocument>
</xsl:template>
<xsl:template match="heading">
<w:p>
<w:pPr><w:pStyle w:val="Heading"/></w:pPr>
<w:r>
<xsl:apply-templates match="italic|bold"/>
</w:r>
</w:p>
<xsl:apply-templates match="heading"/>
</xsl:template>
<xsl:template match="bold">
<w:rPr><w:b w:val="on"/></w:rPr>
<xsl:apply-templates match="text()"/>
<w:rPr><w:b w:val="off"/></w:rPr>
<xsl:apply-templates match="italic|bold"/>
</xsl:template>
<xsl:template match="italic">
<w:rPr><w:i w:val="on"/></w:rPr>
<xsl:apply-templates match="text()"/>
<w:rPr><w:i w:val="off"/></w:rPr>
<xsl:apply-templates match="italic|bold"/>
</xsl:template>
<xsl:template match="text()">
<w:t><xsl:value-of select="."/></w:t>
</xsl:template>
</xsl:stylesheet>它们根本不起作用,XSLT处理器完全省略了“匹配”语句。注意双应用模板是必要的,因为根据段落字符内容的类型,模板有不同的嵌套位置。
常见的错误结果是在WordML文档中获取此类内容。
...
<w:p>
<w:r>
<w:t>run.</w:t>
</w:r>
</w:p>
<w:t>This is </w:t>
...这是完全合法的XML,但完全不能接受,在任何段落之外都有文本,使WordML文档损坏。此外,模板在逻辑上是正确的,如果那些应用-模板和匹配将真正做好他们的工作。
请注意,任何建议(包括抛出所有这些“模板”并在任何标准语言程序中开始)都是可以接受的。
发布于 2012-09-19 07:58:13
我很惊讶您没有收到语法错误,因为下面的XSLT是无效的
<xsl:apply-templates match="italic|bold"/>xsl:apply-templates.的匹配属性无效应该是选择
<xsl:apply-templates select="italic|bold"/>我认为主要问题在于您的粗体和斜体模板
<xsl:template match="bold">
<w:rPr><w:b w:val="on"/></w:rPr>
<xsl:apply-templates match="text()"/>
<w:rPr><w:b w:val="off"/></w:rPr>
<xsl:apply-templates match="italic|bold"/>
</xsl:template> 除了使用match而不是select之外,在关闭w:BE 222元素之后,您还在寻找斜体或粗体元素。你真正需要做的是,这是。
<xsl:template match="bold">
<w:rPr>
<w:b w:val="on"/>
</w:rPr>
<xsl:apply-templates />
<w:rPr>
<w:b w:val="off"/>
</w:rPr>
</xsl:template>因此,不必显式地搜索某些元素,而是搜索任何元素,并有其他模板来处理匹配。
下面是完整的XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xml:space="preserve">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="body">
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns="http://www.w3.org/1999/xhtml" xml:space="preserve">
<w:body>
<xsl:apply-templates select="normal|heading"/>
</w:body>
</w:wordDocument>
</xsl:template>
<xsl:template match="heading">
<w:p>
<w:pPr>
<w:pStyle w:val="Heading"/>
</w:pPr>
<w:r>
<xsl:apply-templates />
</w:r>
</w:p>
</xsl:template>
<xsl:template match="bold">
<w:rPr>
<w:b w:val="on"/>
</w:rPr>
<xsl:apply-templates />
<w:rPr>
<w:b w:val="off"/>
</w:rPr>
</xsl:template>
<xsl:template match="italic">
<w:rPr>
<w:i w:val="on"/>
</w:rPr>
<xsl:apply-templates />
<w:rPr>
<w:i w:val="off"/>
</w:rPr>
</xsl:template>
<xsl:template match="text()">
<w:t>
<xsl:value-of select="."/>
</w:t>
</xsl:template>
</xsl:stylesheet>当应用于示例XML时,输出如下:
<w:wordDocument xml:space="preserve" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns="http://www.w3.org/1999/xhtml">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Heading"/>
</w:pPr>
<w:r>
<w:t> This is the </w:t>
<w:rPr>
<w:b w:val="on"/>
</w:rPr>
<w:rPr>
<w:i w:val="on"/>
</w:rPr>
<w:t> standard </w:t>
<w:rPr>
<w:i w:val="off"/>
</w:rPr>
<w:t> text </w:t>
<w:rPr>
<w:b w:val="off"/>
</w:rPr>
<w:t> run. </w:t>
</w:r>
</w:p>
</w:body>
</w:wordDocument>https://stackoverflow.com/questions/12483294
复制相似问题