Announcement

Collapse
No announcement yet.

XML namespace manipulation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML namespace manipulation

    Mirth Connect V. 3.5.1
    Message Format: XML
    I have unchecked the strip namespaces on source, destination, and template.

    I have found several other posts that deal with namespaces but I have an issue with the namespace on a particular attribute.
    I am reading in a xml file that has 4 in scope namespaces. It is an .xml file generated by MS Excel.

    I cannot figure out how to get the namespaces of the rows and cells correct via a javascript transformer. Here is the declaration of the name spaces and the section of the xml i am trying to alter. The goal is to change the Data value of 1 into some other number say 1000. I am aware the default namespace and the ss are the same. This cannot be changed.

    Code:
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
     xmlns:o="urn:schemas-microsoft-com:office:office"
     xmlns:x="urn:schemas-microsoft-com:office:excel"
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
     xmlns:html="http://www.w3.org/TR/REC-html40">
    <Worksheet>
      <Table>
      <Row ss:AutoFitHeight="0">
        <Cell ss:StyleID="s67">
          <Data ss:Type="Number">1</Data>
        </Cell>
      </Row>
    </Table>
    </Worksheet>
    </Workbook>
    My issue is when i do the following i get a second tag.
    Code:
      msg['Worksheet']['Table']['Row'][5]['Cell'][0]['Data'] = '1000'
      
      Results in 
      
      <Cell ss:StyleID="s67">
          <Data ss:Type="Number">1</Data>
          <ss:Data>1000</ss:Data>
      </Cell>
    A ss:data tag is created and assigned the value instead of changing the existing Data tag.

    but when i access it ignoring namespaces it gains a incorrect namespace.
    Code:
      msg['Worksheet']['Table']['Row'][5]['Cell'][0].*::['Data'] = '1000'
      
      Results in 
      
      <Cell ss:StyleID="s67">
          <Data xmlns="" ss:Type="Number">1</Data>
      </Cell>
    so i attempted to create my own Data tag and add type back with the following but Type doesn't have the proper namespace.
    Code:
      msg['Worksheet']['Table']['Row'][5]['Cell'][0].*::['Data'] = '1000'
      msg['Worksheet']['Table']['Row'][5]['Cell'][0]['Data']['@Type'] = 'Number';
      
      Results in 
      
      <Cell ss:StyleID="s67">
          <Data Type="Number">1</Data>
      </Cell>
    I have found many posts on E4X and namespace issues. But none of them have resolved this issue for me. This post was particularly helpful

    http://www.mirthcorp.com/community/f...ight=namespace

    I have started to play with the template but am not having luck either.

  • #2
    Wow, i have continued to play with this and have discovered my own solution. I decided to work off of a clean tmp and not worry about access issues. I avoid using .*:: because that wrecks the namespace hierarchy.


    Code:
      var reg = new Namespace('', 'urn:schemas-microsoft-com:office:spreadsheet');
      var ss = new Namespace('ss', 'urn:schemas-microsoft-com:office:spreadsheet'); 
    
      tmp['Worksheet']['Table']['Row'][5]['Cell']['Data'] = 1000;
      tmp['Worksheet']['Table']['Row'][5]['Cell']['Data'].setNamespace(reg);
      tmp['Worksheet']['Table']['Row'][5]['Cell']['Data']['@Type'] = "Number";
      tmp['Worksheet']['Table']['Row'][5]['Cell']['Data']['@Type'].setNamespace(ss)
    declare the same namespace, prefix pair as it exists in the xml file. Set the data using what is most likely the wrong namespace. set the attributes using the wrong namespace as well. Then go back and setNamespace to the expected ones on the tag or attribute element. This solution is clean but requires about 3 times more code than expected. I was under the impression i could set namespace while declaring the tag or attribute.

    this syntax doesn't work but tmp['Worksheet']['Table'].reg::[Cell].reg::[Data].ss["Type"] = "Number".


    Is there another syntax or shortcut? Perhaps something easier to read or less verbose?

    Comment

    Working...
    X