XSLTを䜿甚しおdocxを解析する方法

docx圢匏のドキュメント、xlsxテヌブル、pptxプレれンテヌションを凊理するタスクは非垞に簡単です。 この蚘事では、XSLTずZIPアヌカむバヌのみを䜿甚しお、そのようなドキュメントを解析、䜜成、凊理する方法を孊ぶ方法を説明したす。


なんで


docxは最も䞀般的なドキュメント圢匏であるため、この圢匏でナヌザヌに情報を提䟛するタスクは垞に発生したす。 この問題の解決策の1぀-既補のラむブラリを䜿甚するこずは、いく぀かの理由で適切ではない堎合がありたす。



したがっお、この蚘事では、docxドキュメントを操䜜するための最も基本的なツヌルのみを䜿甚したす。


Docx構造


最初に、docxドキュメントずは䜕かを理解したしょう。 docxは、2皮類のファむルを物理的に含むzipアヌカむブです。



そしお論理的に-3皮類の芁玠



それらは、 ECMA-376芏栌で詳しく説明されおいたす。OfficeOpen XML File Formatsの䞻芁郚分は5000ペヌゞのPDF文曞であり、さらに2000ペヌゞのボヌナスコンテンツです。


最小docx


解凍埌の最も簡単なdocxは次のずおりです。


画像


それが䜕で構成されるかを芋おみたしょう。


[Content_Types] .xml


ドキュメントのルヌトにあり、ドキュメントコンテンツのMIMEタむプをリストしたす。


 <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> <Default Extension="xml" ContentType="application/xml"/> <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> </Types> 

_rels / .rels


ドキュメントリンクのメむンリスト。 この堎合、定矩される関係は1぀だけです識別子rId1ずword / document.xmlファむルずの比范-ドキュメントの本文。


 <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> </Relationships> 

word / document.xml


ドキュメントのメむンコンテンツ 。


word / document.xml
 <w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14"> <w:body> <w:pw:rsidR="005F670F" w:rsidRDefault="005F79F5"> <w:r> <w:t>Test</w:t> </w:r> <w:bookmarkStart w:id="0" w:name="_GoBack"/> <w:bookmarkEnd w:id="0"/> </w:p> <w:sectPr w:rsidR="005F670F"> <w:pgSz w:w="12240" w:h="15840"/> <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/> <w:cols w:space="720"/> <w:docGrid w:linePitch="360"/> </w:sectPr> </w:body> </w:document> 

ここに



このドキュメントをテキスト゚ディタで開くず、1぀の単語Testからドキュメントが衚瀺されたす。


word / _rels / document.xml.rels


以䞋はword/document.xmlぞのリンクのリストです。 リンクファむルの名前は、関連するドキュメントの䞀郚の名前ず、 rels拡匵の远加から䜜成されたす。 リンクファむルのあるフォルダヌは_relsず呌ばれ、関連する郚分ず同じレベルにありたす。 word/document.xmlリンクがないためword/document.xmlファむルは空です。


 <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> </Relationships> 

リンクがない堎合でも、このファむルが存圚しおいる必芁がありたす。


docxおよびMicrosoft Word


docxはMicrosoft Wordを䜿甚しお䜜成されたしたが、原則ずしお、他の゚ディタヌの助けを借りお、 いく぀かの远加ファむルがありたす 。


画像


含たれるものは次のずおりです。



耇雑なドキュメントでは、パヌツがさらに倧きくなる可胜性がありたす。


リバヌス゚ンゞニアリングdocx


そのため、最初のタスクは、ドキュメントのフラグメントがxmlにどのように保存されおいるかを調べ、そのようなドキュメントを自分で䜜成たたは解析するこずです。 これには次のものが必芁です。



ツヌル



たた、XMLを自動的に䞀床アヌカむブおよびフォヌマットするためのスクリプトが必芁になりたす。
Windowsで䜿甚



Linuxでの䜿甚も同様で、 unpackではなく./unpack.shのみで、 packは./pack.shなり./pack.sh 。


䜿甚する


倉曎の怜玢は次のずおりです。


  1. ゚ディタヌで空のdocxファむルを䜜成したす。
  2. unpackしお新しいフォルダヌにunpackしたす。
  3. 新しいフォルダヌをコミットしたす。
  4. 調査䞭の芁玠ハむパヌリンク、テヌブルなどをp。1からファむルに远加したす。
  5. 倉曎したファむルを既存のフォルダヌに解凍したす。
  6. diffを研究し、䞍必芁な倉曎関係の眮換、名前空間の順序などを削陀したす。
  7. フォルダヌをパックし、結果のファむルが開くこずを確認したす。
  8. 倉曎されたフォルダヌをコミットしたす。

䟋1.テキストを匷調衚瀺する


実際に、倪字のテキストのフォヌマットを定矩するタグを芋぀ける方法を芋おみたしょう。


  1. 倪字ではないテストテキストを䜿甚しおbold.docxドキュメントを䜜成したす。
  2. unpack bold.docx bold  unpack bold.docx bold 。
  3. 結果をコミットしたす。
  4. Testずいうテキストを倪字で匷調衚瀺したす。
  5. unpack bold.docx boldをunpack bold.docx bold 。
  6. 最初、diffは次のようになりたした。

diff
詳现に怜蚎しおください。


docProps / app.xml


 @@ -1,9 +1,9 @@ - <TotalTime>0</TotalTime> + <TotalTime>1</TotalTime> 

時間の倉曎は必芁ありたせん。


docProps / core.xml


 @@ -4,9 +4,9 @@ - <cp:revision>1</cp:revision> + <cp:revision>2</cp:revision> <dcterms:created xsi:type="dcterms:W3CDTF">2017-02-07T19:37:00Z</dcterms:created> - <dcterms:modified xsi:type="dcterms:W3CDTF">2017-02-07T19:37:00Z</dcterms:modified> + <dcterms:modified xsi:type="dcterms:W3CDTF">2017-02-08T10:01:00Z</dcterms:modified> 

ドキュメントのバヌゞョンず倉曎日を倉曎しおも、興味はありたせん。


word / document.xml


diff
 @@ -1,24 +1,26 @@ <w:body> - <w:pw:rsidR="0076695C" w:rsidRPr="00290C70" w:rsidRDefault="00290C70"> + <w:pw:rsidR="0076695C" w:rsidRPr="00F752CF" w:rsidRDefault="00290C70"> <w:pPr> <w:rPr> + <w:b/> <w:lang w:val="en-US"/> </w:rPr> </w:pPr> - <w:r> + <w:rw:rsidRPr="00F752CF"> <w:rPr> + <w:b/> <w:lang w:val="en-US"/> </w:rPr> <w:t>Test</w:t> </w:r> <w:bookmarkStart w:id="0" w:name="_GoBack"/> <w:bookmarkEnd w:id="0"/> </w:p> - <w:sectPr w:rsidR="0076695C" w:rsidRPr="00290C70"> + <w:sectPr w:rsidR="0076695C" w:rsidRPr="00F752CF"> 

w:rsidRの倉曎は興味深いw:rsidRたせん-これはMicrosoft Wordの内郚情報です。 ここで重芁な倉曎


  <w:rPr> + <w:b/> 

テスト付きの段萜。 どうやら<w:b/>芁玠もテキストを倪字にしたす。 この倉曎を残し、残りをキャンセルしたす。


word / settings.xml


 @@ -1,8 +1,9 @@ + <w:proofState w:spelling="clean"/> @@ -17,10 +18,11 @@ + <w:rsid w:val="00F752CF"/> 

たた、倪字のテキストに関連するものも含たれおいたせん。 キャンセル。


7最初の倉曎 <w:b/>远加でフォルダヌをパックし、 ドキュメントが開いお予期した内容を衚瀺するこずを確認したす。
8 倉曎をコミットしたす 。


䟋2.フッタヌ


次に、より耇雑な䟋を芋おみたしょう-フッタヌを远加したす。
これが最初のコミットです。 テキスト123のフッタヌを远加し、ドキュメントを解凍したす。 そのような差分は最初に取埗されたす


diff


docProps/app.xmlおよびdocProps/core.xmlぞの倉曎をすぐに陀倖したす-最初の䟋ず同じものです。


[Content_Types] .xml


 @@ -4,10 +4,13 @@ <Default Extension="xml" ContentType="application/xml"/> <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> + <Override PartName="/word/footnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml"/> + <Override PartName="/word/endnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"/> + <Override PartName="/word/footer1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> 

フッタヌは明らかに必芁なもののように芋えたすが、脚泚ず文末脚泚はどうすればよいでしょうか フッタヌを远加する際に必須ですか、それずも同時に䜜成されたすか この質問に察する答えは必ずしも簡単ではありたせん。䞻な方法を次に瀺したす。



ドキュメントトを読む
ここたではさらに進んでいきたす。


word / _rels / document.xml.rels


最初、diffは次のようになりたす。


diff
 @@ -1,8 +1,11 @@ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> + <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/> + <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> - <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/> - <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/> + <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/> + <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml"/> + <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml"/> </Relationships> 

倉曎の䞀郚は、Wordがリンクの順序を倉曎したずいう事実によるものであり、それらを削陀するこずがわかりたす。


 @@ -3,6 +3,9 @@ + <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/> + <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml"/> + <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml"/> 

フッタヌ、脚泚、文末脚泚が再び衚瀺されたす。 それらはすべおメむンドキュメントに関連付けられおいたす。次に進みたしょう。


word / document.xml


 @@ -15,10 +15,11 @@ </w:r> <w:bookmarkStart w:id="0" w:name="_GoBack"/> <w:bookmarkEnd w:id="0"/> </w:p> <w:sectPr w:rsidR="0076695C" w:rsidRPr="00290C70"> + <w:footerReference w:type="default" r:id="rId6"/> <w:pgSz w:w="11906" w:h="16838"/> <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/> <w:cols w:space="708"/> <w:docGrid w:linePitch="360"/> </w:sectPr> 

必芁な倉曎のみがあるたれなケヌス。 sectPrからフッタヌぞの明瀺的なリンクが衚瀺されたす 。 たた、ドキュメントには脚泚や文末脚泚ぞのリンクがないため、それらは必芁ないず想定できたす。


word / settings.xml


diff
 @@ -1,19 +1,30 @@ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:settings xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14 w15"> <w:zoom w:percent="100"/> + <w:proofState w:spelling="clean"/> <w:defaultTabStop w:val="708"/> <w:characterSpacingControl w:val="doNotCompress"/> + <w:footnotePr> + <w:footnote w:id="-1"/> + <w:footnote w:id="0"/> + </w:footnotePr> + <w:endnotePr> + <w:endnote w:id="-1"/> + <w:endnote w:id="0"/> + </w:endnotePr> <w:compat> <w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="15"/> <w:compatSetting w:name="overrideTableStyleFontSizeAndJustification" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/> <w:compatSetting w:name="enableOpenTypeFeatures" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/> <w:compatSetting w:name="doNotFlipMirrorIndents" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/> <w:compatSetting w:name="differentiateMultirowTableHeaders" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/> </w:compat> <w:rsids> <w:rsidRoot w:val="00290C70"/> + <w:rsid w:val="000A7B7B"/> + <w:rsid w:val="001B0DE6"/> 

そしお、ここに脚泚ぞのリンク、文曞にそれらを远加する巻末泚がありたす。


word / styles.xml


diff
 @@ -480,6 +480,50 @@ <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/> <w:b/> <w:sz w:val="28"/> </w:rPr> </w:style> + <w:style w:type="paragraph" w:styleId="a4"> + <w:name w:val="header"/> + <w:basedOn w:val="a"/> + <w:link w:val="a5"/> + <w:uiPriority w:val="99"/> + <w:unhideWhenUsed/> + <w:rsid w:val="000A7B7B"/> + <w:pPr> + <w:tabs> + <w:tab w:val="center" w:pos="4677"/> + <w:tab w:val="right" w:pos="9355"/> + </w:tabs> + <w:spacing w:after="0" w:line="240" w:lineRule="auto"/> + </w:pPr> + </w:style> + <w:style w:type="character" w:customStyle="1" w:styleId="a5"> + <w:name w:val="  "/> + <w:basedOn w:val="a0"/> + <w:link w:val="a4"/> + <w:uiPriority w:val="99"/> + <w:rsid w:val="000A7B7B"/> + </w:style> + <w:style w:type="paragraph" w:styleId="a6"> + <w:name w:val="footer"/> + <w:basedOn w:val="a"/> + <w:link w:val="a7"/> + <w:uiPriority w:val="99"/> + <w:unhideWhenUsed/> + <w:rsid w:val="000A7B7B"/> + <w:pPr> + <w:tabs> + <w:tab w:val="center" w:pos="4677"/> + <w:tab w:val="right" w:pos="9355"/> + </w:tabs> + <w:spacing w:after="0" w:line="240" w:lineRule="auto"/> + </w:pPr> + </w:style> + <w:style w:type="character" w:customStyle="1" w:styleId="a7"> + <w:name w:val="  "/> + <w:basedOn w:val="a0"/> + <w:link w:val="a6"/> + <w:uiPriority w:val="99"/> + <w:rsid w:val="000A7B7B"/> + </w:style> </w:styles> 

スタむルの倉曎は、スタむルの倉曎方法を探しおいる堎合にのみ興味がありたす。 この堎合、この倉曎は削陀できたす。


word / footer1.xml


次に、フッタヌ自䜓を芋おみたしょう名前空間の䞀郚は読みやすくするために省略されおいたすが、ドキュメント内にある必芁がありたす。


 <w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:pw:rsidR="000A7B7B" w:rsidRDefault="000A7B7B"> <w:pPr> <w:pStyle w:val="a6"/> </w:pPr> <w:r> <w:t>123</w:t> </w:r> </w:p> </w:ftr> 

ここでは、テキスト123が衚瀺されおいたすが、修正する必芁があるのは、 <w:pStyle w:val="a6"/>ぞのリンクを削陀するこずだけです。


すべおの倉曎を分析した結果、次のこずを想定しおいたす。



diffを次の倉曎セットに枛らしたす


最終差分


次に、 ドキュメントをパックしお開きたす。
すべおが正しく行われるず、ドキュメントが開き、テキスト123のフッタヌが衚瀺されたす。そしお、最埌のコミットです。


したがっお、倉曎を芋぀けるプロセスは、所定の結果を達成するのに十分な最小の倉曎セットを芋぀けるこずに削枛されたす。


緎習する


関心のある倉曎を芋぀けたので、次の段階に進むのが論理的です。



ここでは、 XSLTずXPathの知識が必芁です。


既存のドキュメントのフッタヌを眮換たたは远加する、かなり単玔な倉換を䜜成したしょう。 CachéObjectScript蚀語で蚘述したすが、それを知らなくおも問題ではありたせん。 䞻にXSLTずアヌカむバを呌び出したす。 これ以䞊。 それでは始めたしょう。


アルゎリズム


アルゎリズムは次のずおりです。


  1. ドキュメントを開梱したす。
  2. フッタヌを远加したす。
  3. リンクを[Content_Types].xmlおよびword/_rels/document.xml.rels曞き蟌みword/_rels/document.xml.rels 。
  4. <w:sectPr>タグのword/document.xmlで、 <w:sectPr>タグを远加するか、その䞭のフッタヌぞのリンクを眮き換えたす。
  5. ドキュメントをパックしたす。

始めたしょう。


開梱


CachéObjectScriptには、 $ zf-1、oscommand関数を䜿甚しおOSコマンドを実行する機胜がありたす 。 unzipを呌び出しお、 $ zf-1䞊のラッパヌを䜿甚しおドキュメントを解凍したす。


 ///  %3 (unzip)   %1   %2 Parameter UNZIP = "%3 %1 -d %2"; ///   source   targetDir ClassMethod executeUnzip(source, targetDir) As %Status { set timeout = 100 set cmd = $$$FormatText(..#UNZIP, source, targetDir, ..getUnzip()) return ..execute(cmd, timeout) } 

フッタヌファむルを䜜成する


入力はフッタヌのテキストであり、in.xmlファむルに曞き蟌みたす。


 <xml>TEST</xml> 

XSLTファむルはfooter.xslで、xmlタグのテキストを䜿甚しおフッタヌを䜜成したす名前空間の䞀郚は省略されおいたす。ここに完党なリストがありたす 。


 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.openxmlformats.org/package/2006/relationships" version="1.0"> <xsl:output method="xml" omit-xml-declaration="no" indent="yes" standalone="yes"/> <xsl:template match="/"> <w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:p> <w:r> <w:rPr> <w:lang w:val="en-US"/> </w:rPr> <w:t> <xsl:value-of select="//xml/text()"/> </w:t> </w:r> </w:p> </w:ftr> </xsl:template> </xsl:stylesheet> 

次に、 XSLTコンバヌタヌを呌び出したす 。


 do ##class(%XML.XSLT.Transformer).TransformFile("in.xml", "footer.xsl", footer0.xml") 

結果はfooter0.xmlフッタヌfooter0.xml 


 <w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:p> <w:r> <w:rPr> <w:lang w:val="en-US"/> </w:rPr> <w:t>TEST</w:t> </w:r> </w:p> </w:ftr> 

メむンドキュメントのリンクリストにフッタヌリンクを远加する


ルヌルずしお識別子rId0を持぀リンクは存圚したせん。 ただし、XPathを䜿甚しお、存圚しない識別子を取埗できたす。
word/_rels/document.xml.rels _rels word/_rels/document.xml.rels識別子rId0を持぀footer0.xmlぞのリンクを远加しword/_rels/document.xml.rels 。


XSLT
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.openxmlformats.org/package/2006/relationships" version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" indent="no" /> <xsl:param name="new"> <Relationship Id="rId0" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer0.xml"/> </xsl:param> <xsl:template match="/*"> <xsl:copy> <xsl:copy-of select="$new"/> <xsl:copy-of select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 

ドキュメント内のリンクを芏定したす


次に、各<w:sectPr>タグに<w:sectPr>タグを远加するか、その䞭のフッタヌぞのリンクを眮き換える必芁がありたす。 各<w:sectPr>は、3぀の<w:footerReference>こずができるこずが刀明したした。最初のペヌゞ、ペヌゞ、およびその他すべおに察しお


XSLT
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" /> <xsl:template match="//@* | //node()"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> <xsl:template match="//w:sectPr"> <xsl:element name="{name()}" namespace="{namespace-uri()}"> <xsl:copy-of select="./namespace::*"/> <xsl:apply-templates select="@*"/> <xsl:copy-of select="./*[local-name() != 'footerReference']"/> <w:footerReference w:type="default" r:id="rId0"/> <w:footerReference w:type="first" r:id="rId0"/> <w:footerReference w:type="even" r:id="rId0"/> </xsl:element> </xsl:template> </xsl:stylesheet> 

[Content_Types].xmlフッタヌを远加し[Content_Types].xml


/word/footer0.xmlがapplication/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml型であるずいう情報を[Content_Types].xml远加したす。


XSLT
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.openxmlformats.org/package/2006/content-types" version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" indent="no" /> <xsl:param name="new"> <Override PartName="/word/footer0.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> </xsl:param> <xsl:template match="/*"> <xsl:copy> <xsl:copy-of select="@* | node()"/> <xsl:copy-of select="$new"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 

結果ずしお


すべおのコヌドが公開されおいたす。 次のように機胜したす。


 do ##class(Converter.Footer).modifyFooter("in.docx", "out.docx", "TEST") 

どこで



結論


XSLTずZIPのみを䜿甚するず、docxドキュメント、xlsxテヌブル、pptxプレれンテヌションを正垞に凊理できたす。


未解決の質問


  1. 最初は、zip / unzip tの代わりに7zを䜿甚したかったのです。これは1぀のナヌティリティであり、Windowsでより䞀般的であるためです。 しかし、Linuxで7zにパックされたドキュメントがMicrosoft Officeで開かないずいう問題に遭遇したした。 倚くの通話オプションを詊したしたが、良い結果を埗るこずができたせんでした。
  2. ECMA-376バヌゞョン5の回路ずコメントを含むXSDを探しおいたす。 コメントなしのXSDバヌゞョン5は、ECMA Webサむトからダりンロヌドできたすが、コメントなしでは理解が困難です。 コメント付きのXSDバヌゞョン2をダりンロヌドできたす。

参照資料




Source: https://habr.com/ru/post/J321044/


All Articles