Skip to content

Commit aba0a29

Browse files
author
Steve Canny
committed
xmlch: add BaseOxmlElement.xpath()
1 parent d4559b8 commit aba0a29

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

docx/opc/package.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from .compat import cls_method_fn
1111
from .constants import RELATIONSHIP_TYPE as RT
12-
from .oxml import CT_Relationships, nsmap, serialize_part_xml
12+
from .oxml import CT_Relationships, serialize_part_xml
1313
from .packuri import PACKAGE_URI, PackURI
1414
from .pkgreader import PackageReader
1515
from .pkgwriter import PackageWriter
@@ -301,7 +301,7 @@ def _rel_ref_count(self, rId):
301301
identified by *rId*.
302302
"""
303303
assert self._element is not None
304-
rIds = self._element.xpath('//@r:id', namespaces=nsmap)
304+
rIds = self._element.xpath('//@r:id')
305305
return len([_rId for _rId in rIds if _rId == rId])
306306

307307
# ----------------------------------------------------------------

docx/oxml/parts/numbering.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .. import OxmlElement
88
from ..shared import CT_DecimalNumber
9-
from ..ns import nsmap, qn
9+
from ..ns import qn
1010
from ..xmlchemy import BaseOxmlElement
1111

1212

@@ -181,7 +181,7 @@ def num_having_numId(self, numId):
181181
"""
182182
xpath = './w:num[@w:numId="%d"]' % numId
183183
try:
184-
return self.xpath(xpath, namespaces=nsmap)[0]
184+
return self.xpath(xpath)[0]
185185
except IndexError:
186186
raise KeyError('no <w:num> element with numId %d' % numId)
187187

@@ -195,7 +195,7 @@ def _next_numId(self):
195195
1 and filling any gaps in numbering between existing ``<w:num>``
196196
elements.
197197
"""
198-
numId_strs = self.xpath('./w:num/@w:numId', namespaces=nsmap)
198+
numId_strs = self.xpath('./w:num/@w:numId')
199199
num_ids = [int(numId_str) for numId_str in numId_strs]
200200
for num in range(1, len(num_ids)+2):
201201
if num not in num_ids:

docx/oxml/parts/styles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Custom element classes related to the styles part
55
"""
66

7-
from ..ns import nsmap, qn
7+
from ..ns import qn
88
from ..xmlchemy import BaseOxmlElement
99

1010

@@ -29,7 +29,7 @@ def style_having_styleId(self, styleId):
2929
"""
3030
xpath = './w:style[@w:styleId="%s"]' % styleId
3131
try:
32-
return self.xpath(xpath, namespaces=nsmap)[0]
32+
return self.xpath(xpath)[0]
3333
except IndexError:
3434
raise KeyError('no <w:style> element with styleId %d' % styleId)
3535

docx/oxml/xmlchemy.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import re
1313

14-
from .ns import NamespacePrefixedTag, qn
14+
from .ns import NamespacePrefixedTag, nsmap, qn
1515
from ..compat import Unicode
1616

1717

@@ -138,6 +138,15 @@ def xml(self):
138138
"""
139139
return serialize_for_reading(self)
140140

141+
def xpath(self, xpath_str):
142+
"""
143+
Override of ``lxml`` _Element.xpath() method to provide standard Open
144+
XML namespace mapping (``nsmap``) in centralized location.
145+
"""
146+
return super(BaseOxmlElement, self).xpath(
147+
xpath_str, namespaces=nsmap
148+
)
149+
141150
@property
142151
def _nsptag(self):
143152
return NamespacePrefixedTag.from_clark_name(self.tag)

docx/parts/document.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from ..opc.oxml import serialize_part_xml
1313
from ..opc.package import Part
1414
from ..oxml import parse_xml
15-
from ..oxml.ns import nsmap
1615
from ..shape import InlineShape
1716
from ..shared import lazyproperty, Parented
1817
from ..table import Table
@@ -214,4 +213,4 @@ def add_picture(self, image_descriptor):
214213
def _inline_lst(self):
215214
body = self._body
216215
xpath = './w:p/w:r/w:drawing/wp:inline'
217-
return body.xpath(xpath, namespaces=nsmap)
216+
return body.xpath(xpath)

0 commit comments

Comments
 (0)