Удалите ошибки проверки правописания из WordML - PullRequest
0 голосов
/ 05 декабря 2018

У меня та же проблема, что и Удалите ошибки проверки правописания слов из WordML и объедините узлы - для которых @Rupesh_Kr предоставил предложенный шаблон xsl.Как мне его использовать (недостаточно репутации, чтобы спросить там)?Я надеюсь, что он удаляет теги XML Microsoft Word proofErr w: type = "spellStart" и w: type = "spellEnd", которые разбивают строки в моем документе.В настоящее время я использую XSL для получения более различимых результатов, добавляя возврат каретки, поэтому я попытался заменить его на его и использовать команду "msxsl.exe -xeystem.xml xml.xsl", где xml.xsl содержит его предложение, но получаетследующая ошибка:

Code:   0xc00ce01d
URL:    file:///xml.xsl
Line:   17
Column: 12
Reference to undeclared namespace prefix: 'w'.

xml.xsl содержит его предложение, как показано ниже:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!--
   ! This is an XML to XML transformation intended to be imported into a host
   ! XSLT.  The source .xml file is copied verbatim by default.
   ! The importing XSL Transform should specify xsl:output as xml, and should
   ! contain templates to override the node and attribute match made here so
   ! that it can transform specific portions of the original XML file.
    -->

  <xsl:output method="xml" encoding="utf-8" indent="yes" />

  <!-- ========================================================================
    -->
  <xsl:template match="w:p[w:proofErr]/w:r[1]">
      <w:r>
          <w:t>
          <xsl:value-of select=".."/>
          </w:t>
      </w:r>
  </xsl:template>

  <xsl:template match="w:p[w:proofErr]/w:r[position() > 1]"/>

</xsl:stylesheet>

Ниже приведен пример входного файла, упрощенный удалением многих определений MS Word:

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core">
<w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"></w:ignoreSubtree>
<o:DocumentProperties>
<o:Lines>1</o:Lines>
</o:DocumentProperties>
<w:fonts>
</w:fonts>
<w:body>
<wx:sect>
<w:p>
<w:pPr>
<w:pStyle w:val="BodyText"></w:pStyle>
</w:pPr>
<w:proofErr w:type="spellStart"></w:proofErr>
<w:r>
<w:t>Hellow</w:t>
</w:r>
<w:proofErr w:type="spellEnd"></w:proofErr>
<w:r>
<w:t> </w:t>
</w:r>
<w:proofErr w:type="spellStart"></w:proofErr>
<w:r>
<w:t>world!</w:t>
</w:r>
<w:proofErr w:type="spellEnd"></w:proofErr>
</w:p>
<w:sectPr>
<w:ftr w:type="odd">
</w:ftr>
</w:sectPr>
</wx:sect>
</w:body>
</w:wordDocument>

Желаемый результат будет:

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core">
<w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"></w:ignoreSubtree>
<o:DocumentProperties>
<o:Lines>1</o:Lines>
</o:DocumentProperties>
<w:fonts>
</w:fonts>
<w:body>
<wx:sect>
<w:p>
<w:pPr>
<w:pStyle w:val="BodyText"></w:pStyle>
</w:pPr>
<w:r>
<w:t>Hellow world!</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:ftr w:type="odd">
</w:ftr>
</w:sectPr>
</wx:sect>
</w:body>
</w:wordDocument>

1 Ответ

0 голосов
/ 06 декабря 2018

Во-первых, удаление узлов w:proofErr тривиально: вам просто нужно добавить пустой шаблон, который соответствует им:

<xsl:template match="w:proofErr"/>

Другая проблема объединения всего текста в один узел w:tне так тривиально.То, что я собираюсь сделать, будет работать для данного примера, но может привести к неожиданным результатам для других документов - в частности, для документов с несколькими абзацами (каждый абзац будет содержать весь текст в отдельности).

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="w:t"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- copy all text into the first w:r node -->
<xsl:template match="w:r[1]">
    <xsl:copy>
        <w:t>
            <xsl:for-each select="../w:r">
                <xsl:value-of select="w:t"/>
            </xsl:for-each>
        </w:t>
    </xsl:copy>
</xsl:template>

<!-- remove other w:r nodes -->
<xsl:template match="w:r[position() > 1]"/>

<!-- remove w:proofErr nodes -->
<xsl:template match="w:proofErr"/>

</xsl:stylesheet>

Применительно к вашему примеру ввода результат будет:

<?xml version="1.0" encoding="UTF-8"?>
<w:wordDocument xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
  <w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/>
  <o:DocumentProperties>
    <o:Lines>1</o:Lines>
  </o:DocumentProperties>
  <w:fonts/>
  <w:body>
    <wx:sect>
      <w:p>
        <w:pPr>
          <w:pStyle w:val="BodyText"/>
        </w:pPr>
        <w:r>
          <w:t>Hellow world!</w:t>
        </w:r>
      </w:p>
      <w:sectPr>
        <w:ftr w:type="odd"/>
      </w:sectPr>
    </wx:sect>
  </w:body>
</w:wordDocument>
...