Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:36:08+00:00 2026-05-26T15:36:08+00:00

I would like to replace a text of aliasname element with a text from

  • 0

I would like to replace a text of aliasname element with a text from name 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 aliasname element is replaced by a text from name 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:36:09+00:00Added an answer on May 26, 2026 at 3:36 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little script that replace some text, that come from a xml
In a text, I would like to replace all occurrences of $word by [$word]($word)
I would like to replace drop down list (that has on change event that
I would like to preg_replace some text but only if it's not commented. Here's
I have code like that: TEXT_TO_FILTER='I would like to replace this $var to proper
I have text that has these fancy double quotes: '' and I would like
I would like to replace text with image, but I want also to have
I would like to replace in text-fragments like: <strong>Media Event &quot;New Treatment Options on
I would like to replace symbols with HTMLtags. For example: This :is: some :text:
I would like to change the text of a HTML element but preserve the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.