Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make .nsmap available in XSLT extensions. #269

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/lxml/readonlytree.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,26 @@ cdef class _ReadOnlyElementProxy(_ReadOnlyProxy):
return funicode(self._c_node.ns.prefix)
return None

property nsmap:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a copy of _Element.nsmap, so the dict building should go into a shared helper function.

u"""Namespace prefix->URI mapping known in the context of this
Element.
"""
def __get__(self):
self._assertNode()
cdef xmlNode* c_node
cdef xmlNs* c_ns
nsmap = {}
c_node = self._c_node
while c_node is not NULL and c_node.type == tree.XML_ELEMENT_NODE:
c_ns = c_node.nsDef
while c_ns is not NULL:
prefix = funicodeOrNone(c_ns.prefix)
if prefix not in nsmap:
nsmap[prefix] = funicodeOrNone(c_ns.href)
c_ns = c_ns.next
c_node = c_node.parent
return nsmap

def get(self, key, default=None):
u"""Gets an element attribute.
"""
Expand Down
36 changes: 36 additions & 0 deletions src/lxml/tests/test_xslt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,42 @@ def execute(self, context, self_node, input_node, output_parent):
b'<p style="color:red">This is *-arbitrary-* text in a paragraph</p>\n',
etree.tostring(result))

def test_extensions_nsmap(self):
tree = self.parse("""\
<root>
<inner xmlns:sha256="http://www.w3.org/2001/04/xmlenc#sha256">
<data>test</data>
</inner>
</root>
""")
style = self.parse("""\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="extns" extension-element-prefixes="my" version="1.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="data">
<my:show-nsmap/>
</xsl:template>
</xsl:stylesheet>
""")
class MyExt(etree.XSLTExtension):
def execute(self, context, self_node, input_node, output_parent):
output_parent.text = str(input_node.nsmap)

extensions = { ('extns', 'show-nsmap') : MyExt() }

result = tree.xslt(style, extensions=extensions)
self.assertEqual(etree.tostring(result, pretty_print=True), """\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to compare to a bytes literal to work in Py3.

<root>
<inner xmlns:sha256="http://www.w3.org/2001/04/xmlenc#sha256">{\'sha256\': \'http://www.w3.org/2001/04/xmlenc#sha256\'}
</inner>
</root>
""")



class Py3XSLTTestCase(HelperTestCase):
"""XSLT tests for etree under Python 3"""
Expand Down