My last article looked at parsing an XML string with a known structure and order of elements and attributes. This can be acceptable in some scenarios, but what if the elements or attributes are in a different order? Then it is necessary to use the element and attribute names rather than their index position. This requires a little more complexity to the ShapeSheet functions, and this can result in longer processing time, so needs to be used cautiously, but it is possible, as I demonstrate in this article.
The Document ShapeSheet functions
I reduced the main ShapeSheeet functions to just four, for my purpose, and placed them in the User-defined Cells section of the document ShapeSheet. They can then be used by any shape within the document.
fnGetElement
Function to get the named ELEMENT within provided xml NODE
=IFERROR(
IF(
FIND("<"&ARG("ELEMENT")&">",ARG("NODE"),1,TRUE)>0,
MID(ARG("NODE"),
FIND("<"&ARG("ELEMENT")&">",ARG("NODE"),1,TRUE)+
LEN("<"&ARG("ELEMENT")&">"),
FIND("</"&ARG("ELEMENT")&">",ARG("NODE"),1,TRUE)-
LEN("<"&ARG("ELEMENT")&">")-
FIND("<"&ARG("ELEMENT")&">",ARG("NODE"),1,TRUE)),
MID(ARG("NODE"),
FIND(">",ARG("NODE"),
FIND("<"&ARG("ELEMENT")&" ",ARG("NODE"),1,TRUE))+1,
FIND("</"&ARG("ELEMENT")&">",ARG("NODE"),1,TRUE)-
FIND(">",ARG("NODE"),
FIND("<"&ARG("ELEMENT")&" ",ARG("NODE"),1,TRUE)
,1,TRUE)))
,"")
The FIND(…) function has an optional argument to be case insensitive, and the IFERROR(…) function provides the ability to return an empty string if the FIND(…) fails.
fnGetAttribute
Function to get the named ATTRIB within provided xml NODE
=IFERROR(
IF(
OR(LEN(ARG("ELEMENT"))=0,LEN(ARG("ATTRIB"))=0),
"",
MID(ARG("ELEMENT"),
FIND(" "&ARG("ATTRIB")&"=""",ARG("ELEMENT"),1,TRUE)+
LEN(" "&ARG("ATTRIB")&"="""),
FIND("""",ARG("ELEMENT"),
FIND(" "&ARG("ATTRIB")&"=""",ARG("ELEMENT"),1,TRUE)+
LEN(" "&ARG("ATTRIB")&"=""")+1,TRUE)-
FIND(" "&ARG("ATTRIB")&"=""",ARG("ELEMENT"),1,TRUE)-
LEN(" "&ARG("ATTRIB")&"=""")))
,"")
fnGetElementCount
Function to get the count of the named ELEMENTs within provided xml NODE
=(
LEN(ARG("NODE"))-
LEN(SUBSTITUTE(LOWER(ARG("NODE")),"</"&LOWER(ARG("ELEMENT"))&">",""))
)/
LEN("</"&LOWER(ARG("ELEMENT"))&">")
fnGetElementAtIndex
Function to get the named ELEMENT at index position IDX within provided xml NODE
=IFERROR(
INDEX(ARG("IDX"),
ARG("NODE"),"</"&
ARG("ELEMENT")&">",
REF())&
"</"&ARG("ELEMENT")&">",
"")
The Books master shape
The Books master shape references these functions, and has a Shape Data row, Xml, which can have a suitable XML string pasted into it, either via the Shape Data window or using the ScreenTip dialog. The ScreenTip dialog is the only native window in Visio that provides a multi-line display and has been linked to Prop.Xml with the following SETATREF(…) formula.
=SETATREF(Prop.Xml)
The Books shape also has an extra User-defined Cell, User.ElementCount, returns the number of book elements in the catalog node, used for visibility of each sub-shape:
=EVALCELL(User.fnGetElementCount,"NODE",LOWER(EVALCELL(User.fnGetElement,"ELEMENT",LOWER("catalog"),"NODE",Prop.Xml)),"ELEMENT",LOWER("book"))
The remaining User-defined Cell, User.MaxIndex, contains a formula that is used in the sub-shapes
The book sub-shapes
Each of the 20 sub-shapes have a Prop.Index Shape Data row which has a unique value from 0 to 19 to provide an index into each of the book child elements of the catalog node in the Prop.Xml string of the parent shape. This Prop.Index value is the only cell that needs to be edited when a sub-shape is duplicated. The other ShapeSheet cells automatically evaluate from this value.
The Geometry1.NoShow cell formula simply returns TRUE if the Prop.Index value is greater than the number of book elements in the catalog node.
=GUARD(Prop.Index>=Sheet.5!User.ElementCount)
Each sub-shape represents a book, at the unique, sequential Prop.Index value child book element of the catalog node in the Xml string. There are 20 sub-shapes in my Books shape, which is therefore the maximum number of books that can be displayed. If there are less than 20 book elements in the catalog node, then the indexed sub-shape is automatically zero width and is invisible.
The Width cell formula of each sub-shape simply divides the Width of the Books shape by the number of book elements with a limit of the number of indices I have entered in the Prop.Index.Format cell. Of course, the Width is zero if there is no book element at the Prop.Index value position:
=GUARD(IF(Geometry1.NoShow,0,Sheet.5!Width*1/MIN(Sheet.5!User.ElementCount,LEN(Prop.Index.Format)-LEN(SUBSTITUTE(Prop.Index.Format,";","")))))
The text and the ScreenTip (Comment cell) are evaluated with a formula similar to this:
=EVALCELL(Sheet.5!User.fnGetElement,
"ELEMENT","title",
"NODE",EVALCELL(Sheet.5!User.fnGetElementAtIndex,
"IDX",Prop.Index,
"ELEMENT","book",
"NODE",EVALCELL(Sheet.5!User.fnGetElement,
"ELEMENT","catalog",
"NODE",Sheet.5!Prop.Xml)
)
)
Only the first ELEMENT argument, title in the above example, needs to be changed to get a different child element value of the indexed book element.
Example Xml
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
Example document
Related articles
Synchronizing Visio Shape Fill Color (or almost any cell) across pages
I was recently asked how the color of one shape can be changed and for other shapes to be automatically updated to the same color … even if they are on different pages! Well, it is possible with Microsoft Visio’s awesome ShapeSheet formulas. In fact, this capability is not limited to the FillForegnd cell ……
Positioning Visio Shape Text Block with a Control Handle
I was recently asked how a control handle can be added to a Visio shape so that it can be used to re-position the text block. Fortunately, it is extremely easy to setup, and requires just two formulas to be updated in the ShapeSheet. This is a great use of the SETATREF(…) function. (more…)
Understanding Segments of Visio Geometry
I recently had to revise my understanding of the POINTALONGPATH(…) function in Visio because I was getting a #REF! error in some cases. My particular scenario requires a line with a number of vertices that are initially all in a straight line but can be moved by dragging controls around that each vertex is bound…
Custom Color Themes in Visio?
I was recently looking into custom color themes for corporate branding in desktop Microsoft Visio and became re-aware how different Visio still is from the rest of the Microsoft Office applications. A Visio page or document does not need to have any theme applied, but the documents of the other Office applications always have a…
When is a Visio Callout not a Callout?
I have been a Visio user/developer since the mid-1990’s and seen the word “callout” used as part of the name of many master shapes in Visio. The images below show five ways that the term “callout” has been applied to the name of Visio master shapes. Generally, each evolution has been an advance on the…
Using Visio Color by Value on Connectors
Data Graphics in Visio Plan 2 and Visio Professional is great, but it only enables us to use them with 2D shapes in Visio, i.e. not on connectors. So, what if you want to change the line colour of the connectors between the 2D shapes because of the data flowing between them? Well, it is…