0

我正在使用 jython、poi 和 jcabi 来解析 pptx 文档。

目标是找出文档中的任何动画定义

这是片段。

import sys
from java.io import FileInputStream,
from com.jcabi.xml import XMLDocument

ffn = os.path.abspath(fn)
fis = FileInputStream(ffn)
ppt = XMLSlideShow(fis)

p = ppt.getSlides()[0]

xml = p.getXmlObject()
doc = XMLDocument(str(xml))

print doc.xpath("/*/p:anim")

在最后一行(print语句)我有一个例外

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Invalid XPath query '/*/p:anim' at com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl: javax.xml.transform.TransformerException: Prefix must resolve to a namespace: p

我之前添加了以下行,print 但错误仍然存​​在

doc.registerNs('p', 'http://schemas.openxmlformats.org/presentationml/2006/main')

我的问题:

如何删除“前缀必须解析”错误消息?

更多信息:

这是 xml 文档的示例片段

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <xml-fragment xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" 
xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" 
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
...
  <p:set>
                                      <p:cBhvr>
                                        <p:cTn dur="1" fill="hold" id="6">
                                          <p:stCondLst>
                                            <p:cond delay="0"/>
                                          </p:stCondLst>
                                        </p:cTn>
                                        <p:tgtEl>
                                          <p:spTgt spid="2"/>
                                        </p:tgtEl>
                                        <p:attrNameLst>
                                          <p:attrName>style.visibility</p:attrName>
                                        </p:attrNameLst>
                                      </p:cBhvr>
                                      <p:to>
                                        <p:strVal val="visible"/>
                                      </p:to>
                                    </p:set>
                                    <p:anim calcmode="lin" valueType="num">
                                      <p:cBhvr additive="base">
                                        <p:cTn dur="500" fill="hold" id="7"/>
                                        <p:tgtEl>
                                          <p:spTgt spid="2"/>
                                        </p:tgtEl>
                                        <p:attrNameLst>
                                          <p:attrName>ppt_x</p:attrName>
                                        </p:attrNameLst>
                                      </p:cBhvr>
                                      <p:tavLst>
                                        <p:tav tm="0">
                                          <p:val>
                                            <p:strVal val="#ppt_x"/>
                                          </p:val>
                                        </p:tav>
                                        <p:tav tm="100000">
                                          <p:val>
                                            <p:strVal val="#ppt_x"/>
                                          </p:val>
                                        </p:tav>
                                      </p:tavLst>
                                    </p:anim>

这是我用来管理依赖关系的 pom.xmlmaven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.utils</groupId>
  <artifactId>ppt</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ppt-viewer</name>
  <url>http://maven.apache.org</url>
  <properties>
    <target.file></target.file>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution><goals><goal>exec</goal></goals></execution>
        </executions>
        <configuration>
          <executable>jython</executable>
          <arguments>
            <argument>-J-cp</argument>
            <classpath/>
            <argument>use_poi.jy</argument>
            <argument>${target.file}</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>com.jcabi</groupId>
      <artifactId>jcabi-xml</artifactId>
      <version>0.21.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-scratchpad</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-excelant</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.xmlbeans</groupId>
      <artifactId>xmlbeans</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-compress</artifactId>
      <version>1.18</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>ooxml-schemas</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-collections4</artifactId>
      <version>4.2</version>
    </dependency>
  </dependencies>
</project>
4

1 回答 1

1

你需要改变

doc.registerNs('p', 'http://schemas.openxmlformats.org/presentationml/2006/main')

XML xml = doc.registerNs('p', 'http://schemas.openxmlformats.org/presentationml/2006/main')

xml并对具有注册命名空间的对象执行操作。指令本身对doc对象没有任何作用。它返回一个带有已注册命名空间的对象。

干杯!

于 2018-11-05T16:39:21.597 回答