0

私は、これらの XSL WordML XML の「世界」に数週間住んでいますが、いわゆる「XSLT プロセッサ」が物事を処理する方法に本当にがっかりしています。

古い質問に関しては、その目的は (まだ簡単だと言えるのであれば) Light Word XML ファイルを整形式の 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の段落スタイル要素は 'normal' と 'heading' (ここでは見出しのみ) であり、'w:p' タグに含まれています。
  • WordMLの文字実行スタイル要素は、「斜体」、「太字」、および「下線」 (ここでは斜体と太字のみ) であり、「w:r」タグに含まれています。
  • WordMLテキストノードは「w:t」タグに含まれています

したがって、予想される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プロセッサは「一致」文を完全に省略します。段落の種類と文字の内容に応じて、テンプレートの入れ子の場所が異なるため、二重の apply-template が必要であることに注意してください。

通常の誤った結果は、WordML ドキュメント内でこの種のコンテンツを取得することです。

    ...
    <w:p>
        <w:r>
            <w:t>run.</w:t>
        </w:r>
    </w:p>    

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

これは完全に正当な XML ですが、段落の外側にテキストがあるため、WordML 文書が破損するため、まったく受け入れられません。その上、apply-template と match が本当に適切な仕事をするなら、テンプレートは論理的に正しいです。

どんな提案 (これらすべての「テンプレート」をスローして標準言語プログラムからやり直すことを含む) も受け入れられます。

4

1 に答える 1

3

次の XSLT が無効であるため、構文エラーが発生していないことに驚いています。

 <xsl:apply-templates match="italic|bold"/>

xsl:apply-templatesmatch属性は無効です。選択する必要があります

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

selectの代わりにmatchを使用するだけでなく、 w:b要素を閉じた後、斜体または太字の要素を探しています。あなたが本当にしなければならないことは、これです。

<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>
于 2012-09-19T07:58:13.407 に答える