0

I have looked through many namespace documents on here and am only able to slightly relate to a few. in my document, I have 3 defaults and only one colon style xmlns, example:

xmlns="someurlNo1"
xmlns:spatial="someurlNo2"
xmlns="someurlNo3"
xmlns="someurlNo4"

From what I have read, it seems that I have 3 defaults (please correct me if I am interpreting this wrong), but when I modify my base xml and then write my new xml, I am only able to avoid having the first two, ns0 and ns1, not show up by commenting out the last two, which makes everything else part of the last two defaults are labeled with "ns2" and "ns3" even if I register all as such:

ET.register_namespace('',"someurlNo0") #ns0
ET.register_namespace('spatial',"someurlNo1") #ns1
#ET.register_namespace('',"someurlNo2") #ns2
#ET.register_namespace('',"someurlNo3") #ns3

Does anyone know how to register the last two default namespaces correctly? When I leave the last two not commented out, ns0 and ns2 appear where they should, and while all the ns3s disappear, the default is no longer equal to "someurlNo3". This answer: https://stackoverflow.com/a/43530940 has so far been the most helpful explanation to me in illustrating that there may be multiple defaults that travel down (which I believe is true for my document), but I am still unsure how to properly register them. Any ideas would be much appreciated!

Here is what the top part of my xml looks like that includes all 4 namespaces. I'd rather spare you from seeing all 3k lines but if needed I can share more:

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" spatial:required="true" version="1" xmlns:spatial="http://www.sbml.org/sbml/level3/version1/spatial/version1">
  <notes>
    <body xmlns="http://www.w3.org/1999/xhtml">
     <p>Exported by VCell 7.3</p>
  </body>
  </notes>
  <model areaUnits="um2" extentUnits="molecules" id="_zero_6_29_21_Phase1_cellularConcAgain_Spatial" lengthUnits="um" name="06_29_21_Phase1_cellularConcAgain_Spatial" substanceUnits="molecules" timeUnits="s" volumeUnits="um3">
    <spatial:geometry xmlns:spatial="http://www.sbml.org/sbml/level3/version1/spatial/version1" id="vcell" spatial:coordinateSystem="cartesian" spatial:id="vcell">
      <spatial:listOfCoordinateComponents>
        <spatial:coordinateComponent id="x" spatial:id="x" spatial:type="cartesianX" spatial:unit="um">
          <spatial:boundaryMin id="Xmin" spatial:id="Xmin" spatial:value="0.0"/>
          <spatial:boundaryMax id="Xmax" spatial:id="Xmax" spatial:value="1.6"/>
        </spatial:coordinateComponent>
        <spatial:coordinateComponent id="y" spatial:id="y" spatial:type="cartesianY" spatial:unit="um">
          <spatial:boundaryMin id="Ymin" spatial:id="Ymin" spatial:value="0.0"/>
          <spatial:boundaryMax id="Ymax" spatial:id="Ymax" spatial:value="3.5"/>
        </spatial:coordinateComponent>
      </spatial:listOfCoordinateComponents>
      <spatial:listOfDomains>
        <spatial:domain id="chr0" spatial:domainType="domainType_chr" spatial:id="chr0">
          <spatial:listOfInteriorPoints>
            <spatial:interiorPoint spatial:coord1="0.0" spatial:coord2="0.0" spatial:coord3="5.0"/>
          </spatial:listOfInteriorPoints>
        </spatial:domain>
      </spatial:listOfDomains>
      <spatial:listOfDomainTypes>
        <spatial:domainType id="domainType_chr" spatial:id="domainType_chr" spatial:spatialDimensions="3"/>
      </spatial:listOfDomainTypes>
      <spatial:listOfGeometryDefinitions>
        <spatial:analyticGeometry id="Analytic_Geometry1640227629" spatial:id="Analytic_Geometry1640227629" spatial:isActive="true">
          <spatial:listOfAnalyticVolumes>
            <spatial:analyticVolume spatial:domainType="domainType_chr" spatial:functionType="layered" spatial:id="chr" spatial:ordinal="0">
              <math xmlns="http://www.w3.org/1998/Math/MathML">              
                <apply>
                  <neq/>
                  <cn> 0 </cn>
                  <cn> 1 </cn>
                </apply>
              </math>
4
  • Please provide a minimal reproducible example. Show us the input XML, the wanted output XML and the actual output XML. Commented Jul 16, 2021 at 18:35
  • @mzjn thank you for your comment, do you have any suggestions for learning how to navigate/learn more about this: "However, an element can have multiple declarations for namespaces associated with prefixes." ? Commented Jul 16, 2021 at 18:57
  • Sorry, I removed the comment after having seen what your input XML looks like. It is not complete (end tags missing), but I understand where the namespaces are defined. It looks OK. But what is the problem? How exactly are you modifying the XML document? And what is the result? Commented Jul 16, 2021 at 19:22
  • 1
    I think I see what the issue might be. ElementTree does not leave namespace declarations unchanged. In short, the easiest way to solve this is probably to use lxml instead. See stackoverflow.com/q/45990761/407651 Commented Jul 16, 2021 at 19:38

1 Answer 1

0

You're misunderstanding namespaces. That a namespace is "default" is not a property of that namespace, it's a property of the XML in that location.

Don't get distracted. Give all namespace URIs that you're going to use a prefix, done.

ns = {
    'a': 'someurlNo0',
    'b': 'someurlNo1',
    'c': 'someurlNo2',
    'd': 'someurlNo3',
    'e': 'someurlNo3',   # same URI as above, perfectly legal
}

tree = ET.parse('path/to.xml')

tree.findall("./a:node/b:node/c:node/d:node/e:node", namespaces=ns)

It does not even need to be the same prefix as in your XML. In fact, avoid that. Give namespace URIs prefixes that make reading your code easy. All that matters in the end is the namespace URI, the prefix is ephemeral.

As long as the prefixes in your code resolve to the actual namespace URI of the targeted nodes, you're good. It does not matter if the namespaces are default at the specific location in the XML.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.