我正在使用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>
这是我用来通过maven
管理依赖项的pom.xml
<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>
答案 0 :(得分: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
对象没有任何作用。它返回带有已注册名称空间的对象。
干杯!