Question

For Crowdflower I need to define a CML and as it is very similar to XML I want to use LXML for this task. Crowdflower defines for it's CML tags like:

  • <cml:textarea label="Sample text area:" /> or
  • <cml:checkbox label="A single checkbox with a default value" />

(Taken from the Crowdflower website: [1])

Moreover, these CML files usually don't use a root element. When I try to create a CML element with lxml it raises a XMLSyntaxError. I would like to use the solution from Is there a switch to ignore undefined namespace prefixes in LXML?, but the created CMLs lack of the root.

How can I modify LXML that it does ignore unknown namespace prefixes?

Code

from lxml import etree

txt = "An example label"
txt_elm = etree.fromstring('<cml:text label="%s" />' % txt)

Error Message

txt_elm = etree.fromstring('<cml:text label="%s" />' % txt)
File "lxml.etree.pyx", line 2994, in lxml.etree.fromstring (src/lxml/lxml.etree.c:63296)
File "parser.pxi", line 1617, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:93649)
File "parser.pxi", line 1495, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:92453)
File "parser.pxi", line 1011, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:89097)
File "parser.pxi", line 577, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:84804)
File "parser.pxi", line 676, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:85904)
File "parser.pxi", line 616, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:85228)
lxml.etree.XMLSyntaxError: Namespace prefix cml on text is not defined, line 1, column 16

Example 1

<h2>Image 1 :</h2>
<img src="{{urlmedia1}}" width="450" height="300">

<h2>Image 2 :</h2>
<img src="{{urlmedia2}}" width="450" height="300">

<cml:radios label="Which one of these two images conveys the most {{axis}} ?" validates="required">
   <cml:radio label="Image 1"/>
   <cml:radio label="Image 2"/>
</cml:radios>

Example 2

<p>Focus on the emotion you feel when watching these shots. Which one convey?    <b>{{axis}}</b> the most ?</p>
<p>While you have the right to watch the shots as many times as you want, you should focus on your first impression.</p>
<hr/>
    <p>video 1: <em>{{miscdata1}}</em></p>
    <object style="height: 300px; width: 490px">
      <param name="movie" value="{{urlmedia1}}" />
      <param name="allowFullScreen" value="true" />
      <param name="allowScriptAccess" value="always" />
      <embed src="{{urlmedia1}}" type="application/x-shockwave-flash" allowfullscreen="false" allowScriptAccess="always" width="490" height="300"/>
    </object>
<hr/>
    <p>video 2: <em>{{miscdata2}}</em></p>
    <object style="height: 300px; width: 490px">
      <param name="movie" value="{{urlmedia2}}" />
      <param name="allowFullScreen" value="true" />
      <param name="allowScriptAccess" value="always" />
      <embed src="{{urlmedia2}}" type="application/x-shockwave-flash" allowfullscreen="false" allowScriptAccess="always" width="490" height="300"/>
    </object>
<hr/>
<cml:radios label="Which one conveys {{axis}} the most?" validates="required">
  <cml:radio label="Shot 1" />
  <cml:radio label="Shot 2" />
</cml:radios>

Both examples are taken from: 2

NOTE: My goal is to build a CML structure, not to parse one.

Was it helpful?

Solution

You can kind of make this easier using ElementMaker (from lxml) like this:

from lxml.builder import ElementMaker
E = ElementMaker(namespace='cml', nsmap={'cml':'cml'})

cmlroot = E.root()
radios = E.radios(label='My question?', validates='required')
cmlroot.append(radios)
radios.append(E.radio(label='Image1'))
radios.append(E.radio(label='Image2'))

etree.dump(cmlroot)
<cml:root xmlns:cml="cml">
  <cml:radios validates="required" label="My question?">
    <cml:radio label="Image1"/>
    <cml:radio label="Image2"/>
  </cml:radios>
</cml:root>

You can then get the text you want by stripping off the first and last line.

There are other ways to deal with it, but i think this is the easiest.

This doesn't help with parsing, which is its own ball of issues, but works fine for generation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top