首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将意大利语XML标记转换为WordML标记II

将意大利语XML标记转换为WordML标记II
EN

Stack Overflow用户
提问于 2012-09-18 19:00:42
回答 1查看 1.7K关注 0票数 0

我已经住在这些XSL“世界”中了几个星期了,我对所谓的‘WordML处理器’处理事情的方式感到非常不满。

对于一个老问题,其目的是(如果-we仍然可以称之为简单的话)将Light文件转换为格式良好的WordML文件。

对这个问题我很抱歉,但我想没有其他解释的方法了。

我有以下XML文档

代码语言:javascript
复制
<?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:P”标记中。
  • WordML字符运行样式元素是‘斜体’、‘粗体’和‘下划线’(这里只有斜体和粗体),并且包含在'w:r‘标签中。
  • WordML文本节点被“w:t”标记所包含

因此,预期的WordML文档输出如下:

代码语言:javascript
复制
<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模板文件(将由您的反馈进行更正):

代码语言:javascript
复制
<?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文档中获取此类内容。

代码语言:javascript
复制
    ...
    <w:p>
        <w:r>
            <w:t>run.</w:t>
        </w:r>
    </w:p>    

    <w:t>This is </w:t>
    ...

这是完全合法的XML,但完全不能接受,在任何段落之外都有文本,使WordML文档损坏。此外,模板在逻辑上是正确的,如果那些应用-模板和匹配将真正做好他们的工作。

请注意,任何建议(包括抛出所有这些“模板”并在任何标准语言程序中开始)都是可以接受的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-19 07:58:13

我很惊讶您没有收到语法错误,因为下面的XSLT是无效的

代码语言:javascript
复制
 <xsl:apply-templates match="italic|bold"/>

xsl:apply-templates.的匹配属性无效应该是选择

代码语言:javascript
复制
<xsl:apply-templates select="italic|bold"/>

我认为主要问题在于您的粗体斜体模板

代码语言:javascript
复制
<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元素之后,您还在寻找斜体或粗体元素。你真正需要做的是,这是。

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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时,输出如下:

代码语言:javascript
复制
<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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12483294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档