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
Using Notepad++ to Edit Visio ShapeSheet formulas
Visio shapes get their smartness from the formulas that are entered into the ShapeSheet, but editing these formulas can be extremely tricky and prone to error because of the lack of a modern programmer’s interface. Formulas can be quite long (up to 64k characters) but even medium size ones like the one in User.GetWorkdays cell…
Adding a second Function header bar to Visio swimlanes
I was recently asked if a second function header bar can be added to the swimlanes in the cross-functional flowchart templates in Visio. Some swimlanes can get quite wide, so it can be useful to have a duplicate function header shape on the far-side too. It is quite simple to duplicate the existing function header…
Parsing XML data in Visio Shapes
One of my current projects uses XML data, and some of the values in the XML data control the display and content of Visio shapes. Therefore, I looked deeper into how the XML data can be used directly to control parts of the graphics. Although the external data linking feature in Visio Professional and Visio…
Highlighting dirty text in Visio shapes
I suppose I should explain what I mean by dirty text first 🙂 I often display the value of a Shape Data row in Visio text, but sometimes the solution requires that the value is also editable. This is ok if the client accepts that all editing is done with the Shape Data window or…
My new book on Visualizing Processes with Microsoft Visio has launched
Back in the early 1990s, there was an application called ABC Flowcharter that was the market leader for diagramming business flowcharts, but some of the brains behind Aldus PageMaker saw an opportunity to create something smarter, and left to write the Visio product, with the stated aim to overtake ABC Flowcharter within 2 years. They…
Interactive Demographics of the European Union in Visio
I was recently contacted by a reader, Stanley M. Max (Towson University lecturer), who had started creating a Visio map of the 27 countries in the European Union in 2020, along with the demographics culled from Wikipedia. He wanted to know if the map and stats could be combined to make a more appealing presentation.…
Leave a Reply