0

I would like to replace a text of element with a text from if there is no corresponding key in the dictionary.

here is the xml that I am working on now.

- <esri:Workspace xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  - <WorkspaceDefinition xsi:type="esri:WorkspaceDefinition">   
         <WorkspaceType>esriLocalDatabaseWorkspace</WorkspaceType>       
         <Domains xsi:type="esri:ArrayOfDomain" />  
  - <DatasetDefinitions xsi:type="esri:ArrayOfDataElement">   
          - <DataElement xsi:type="esri:DEFeatureClass">   
            <CatalogPath>/FC=CHO_H12</CatalogPath>      
            <Name>CHO_H12</Name>      
            <DatasetType>esriDTFeatureClass</DatasetType>
            <DSID>8</DSID>      
            <Versioned>false</Versioned>  
            <CanVersion>false</CanVersion>
            <ConfigurationKeyword />
            <HasOID>true</HasOID>
            <OIDFieldName>OBJECTID</OIDFieldName>
            - <Fields xsi:type="esri:Fields"> 
               - <FieldArray xsi:type="esri:ArrayOfField">     
                  - <Field xsi:type="esri:Field">  
                         <Name>KEY_CODE</Name>   
                         <Type>esriFieldTypeString</Type>
                         <IsNullable>true</IsNullable> 
                         <Length>255</Length> 
                         <Precision>0</Precision>
                         <Scale>0</Scale>  
                         <AliasName>リンクコード</AliasName>   # to "KEY_CODE" 
                         <ModelName>KEY_CODE</ModelName>        
                     </Field>
                  - <Field xsi:type="esri:Field"> 
                          <Name>KEN</Name>
                          <Type>esriFieldTypeString</Type> 
                          <IsNullable>true</IsNullable>
                          <Length>255</Length>
                          <Precision>0</Precision>
                          <Scale>0</Scale>
                          <AliasName>都道府県番号</AliasName>    # to "Prefecture_Code"
                          <ModelName>KEN</ModelName>          
                     </Field>
                  - <Field xsi:type="esri:Field">
                           <Name>CITY</Name>          
                           <Type>esriFieldTypeString</Type>          
                           <IsNullable>true</IsNullable>          
                           <Length>255</Length>          
                           <Precision>0</Precision>          
                           <Scale>0</Scale>          
                           <AliasName>市区町村番号</AliasName>   # to "City_code"      
                           <ModelName>CITY</ModelName>          
                           </Field>

I wrote;

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import xml.etree.cElementTree as etree

jp2en = {'リンクコード': 'KEY_CODE', '都道府県番号': 'Prefecture_Code',
'市区町村番号': 'City_Code'} 

    tree = etree.parse('input.xml')
    for field in tree.iterfind('.//Fields/FieldArray/Field'):
         name, alias = [field.find(tag) for tag in ['Name', 'AliasName']]
         if name is None or alias is None: continue
         alias.text = jp2en.get(name.text.strip(), name.text)
    tree.write('output.xml', encoding='utf-8')

However, for the output, a text of element is replaced by a text from even when there is corresponding key in the dictionary, jp2en like this;

<AliasName>リンクコード</AliasName>   Returns a text "KEY_CODE" # the text is the same for Name and a key. 
<AliasName>都道府県番号</AliasName>   Returns a text "KEN" from Name instead of a key "Prefecture_Code"
<AliasName>市区町村番号</AliasName>   Returns a text "CITY" from Name instead of a key "City_code"

I use python 3.2.2.

5
  • What happens when you run the code? What do you expect to happen? Commented Nov 4, 2011 at 17:01
  • You don't need to specify coding: utf-8 in Python 3. It is a default. Commented Nov 5, 2011 at 1:06
  • use <xmltag> instead of [tag:xmltag]. The latter is for StackOverflow tags, not xml tags. Commented Nov 5, 2011 at 1:09
  • shouldnt it be alias.text = jp2en.get(alias.text, name.text)? alias.text (i.e. <AliasName> element) seems to have keys for jp2en Commented Nov 5, 2011 at 1:18
  • for the sake of debugging can you create test file which you believe the values should be changed, and change jp2en.get(whatever) to jp2en[whatever], then run code? it may throw you exception that key is not found and that may give you clue where you have problem Commented Nov 5, 2011 at 1:26

1 Answer 1

1

I guess (seeing that the current behaviour is not what you want) you'd like to replace a text of <AliasName> element with a text from <Name> if there is no corresponding key in the dictionary. Change this line (which doesn't change <AliasName> if there is no key):

 alias.text = jp2en.get(name.text.strip(), alias.text)

To:

 alias.text = jp2en.get(name.text.strip(), name.text)

If key values should come from <AliasName> instead of <Name> then replace the above line by:

 if alias.text is not None:
    alias.text = jp2en.get(alias.text.strip(), name.text)
 else:
    alias.text = name.text
Sign up to request clarification or add additional context in comments.

2 Comments

@Sebastian: I am almost there. It is just that alias text is always replaced by name text even when there is corresponding key in dic..I updated the code I used and output above.
@Sebastian: It did the trick!!!! You are a life saver! Made my work so much easier. Can not appreciate you enough. Thank you!!

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.