【问题标题】:Convert Italic XML Tags into WordML Tags将斜体 XML 标签转换为 WordML 标签
【发布时间】:2012-09-10 05:57:50
【问题描述】:

我只需要将 XML 文档转换为 WordML 文档(如果可以称之为简单的话!),使用此表单(无需处理指令):

<body>
    <p>
        <r>This is the <italic>standard</italic> text run.</r> 
    </p>
</body>

根据 WordML 文档,转换后的 XML 应该如下所示:

<w:body>
    <w:p>
        <w:r>
            <w:t>This is the </w:t> 
        </w:r>
    </w:p>
    <w:p>
        <w:pPr>
            <w:i/>
        </w:pPr>
        <w:r>
            <w:t>standard</w:t> 
        </w:r>
    </w:p>
    <w:p>
        <w:r>
            <w:t> text run.</w:t> 
        </w:r>
    </w:p>
</w:body>

我应该如何创建 XSLT 转换以正确处理斜体标签??..

【问题讨论】:

    标签: xml xslt wordml


    【解决方案1】:

    这种转变

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:w="some:w" exclude-result-prefixes="w">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="body">
      <w:body>
       <xsl:apply-templates/>
      </w:body>
     </xsl:template>
    
     <xsl:template match="p/r/text()">
        <w:p>
            <w:r>
                <w:t><xsl:value-of select="."/></w:t>
            </w:r>
        </w:p>
     </xsl:template>
    
     <xsl:template match="p/r/italic/text()">
        <w:p>
            <w:pPr>
                <w:i/>
            </w:pPr>
            <w:r>
                <w:t><xsl:value-of select="."/></w:t>
            </w:r>
        </w:p>
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时:

    <body>
        <p>
            <r>This is the <italic>standard</italic> text run.</r>
        </p>
    </body>
    

    产生想要的正确结果:

    <w:body xmlns:w="some:w">
       <w:p>
          <w:r>
             <w:t>This is the </w:t>
          </w:r>
       </w:p>
       <w:p>
          <w:pPr>
             <w:i/>
          </w:pPr>
          <w:r>
             <w:t>standard</w:t>
          </w:r>
       </w:p>
       <w:p>
          <w:r>
             <w:t> text run.</w:t>
          </w:r>
       </w:p>
    </w:body>
    

    【讨论】:

    • 我非常荣幸,Dimitre,您的回答...我可以说我已经学习了 XSL,因为您在 StackOverflow.com 上的回答!!尽管如此,我仍然对身份转换完全感到困惑。在示例中,p/r/ 子句很好地处理了格式,但我不知道如何处理稍微复杂的标签使用。顺便说一句,我已经将答案分配给了 Mads,因为他在这里早了几秒钟 :)。
    • @hypfco,不客气。至于更困难的情况——请提出新问题:)
    • @hypfco,对于当前的问题,你真的不需要身份模板。
    【解决方案2】:

    对于像提供的示例一样简单的输入,以下样式表可以工作。使用修改后的identity transform&lt;italics&gt;r/text() 的专用模板。

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
            xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
        <xsl:output indent="yes"/>
    
        <xsl:template match="@*|node()">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:template>
    
        <xsl:template match="body">
            <w:body>
                <xsl:apply-templates/>
            </w:body>
        </xsl:template>
    
      <xsl:template match="r/text()">
          <w:p>
              <w:r>
                  <w:t><xsl:value-of select="."/></w:t> 
              </w:r>
          </w:p>
      </xsl:template>
    
        <xsl:template match="r/italic">
            <w:p>
                <w:pPr>
                    <w:i/>
                </w:pPr>
                <w:r>
                    <w:t><xsl:value-of select="."/></w:t> 
                </w:r>
            </w:p>
        </xsl:template>  
    
    </xsl:stylesheet>
    

    【讨论】:

    • 感谢您的及时回复。也感谢维基百科链接。我已经研究了您的代码以在我的情况下应用它。我还要检查那些身份转换............
    猜你喜欢
    • 2016-06-12
    • 2023-04-05
    • 1970-01-01
    • 2017-12-25
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2019-07-19
    相关资源
    最近更新 更多