Select to view content in your preferred language

Splitting dynamic text strings with XML tags

07-28-2017 10:00 AM
New Contributor III

Is there a way to extract parts of text from a dynamic text using xml tags? What I would like to do is to extract the number from an mxd file name and write it to a text box inside arcmap which is the caption. Example:

filename: Fig_3_Test.mxd

caption: <dyn type="document" property="name" split text start =3 end = 5/>

Thanks in advance


0 Kudos
4 Replies
MVP Honored Contributor


This is an interesting question. It might be best asked in a different space since the GeoNet Help is for help with the geonet. Maybe move it to‌ or‌ ?

0 Kudos
Occasional Contributor III

Hi Nahide,

from a quick scan that sounds like Python 🙂
If I got it right you know the NAME of the element you want to fill, or can create one - then you should be able to use the TextElement Class. Dont think you can just use dynamic text.

Sure - you have to get the string part. If the document name has a structure as you say, something like this should do:

import arcpy,os
mxd = arcpy.mapping.MapDocument("CURRENT")
#do this in two lines - safe bet if Path can contain additional underscores
theName= str(mxd.filePath.split(os.sep)[-1:])
# now extract the number from the mxd-name
theTitle = str(theName.split("_")[-2]) #I assume a logic of _Number_ here
# and given you have an element called map title ...
elm = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "MapTitle")[0]
# set the text
elm.text = TheTitle

New Contributor III

Hi Günter:

I thought so too. But I wanted try my luck to see if anybody was able to do something like that through XML tags. I did something different in the end. I used the dynamic txt tag field for the figure numbers and when they change the figure numbers, which they will, I will do something like this:

for mxd in MXDList:
   fileName = os.path.splitext(os.path.basename(mxd))[0]

   figNum = str(fileName.split("_") [-2]

   InMxd = arcpy.mapping.MapDocument(mxd)

   InMxd.tags = figNum

   InMXD. save

Occasional Contributor III

There you go! 🙂  Thanks for commenting again. Have fun!  Guenter

0 Kudos