Graphics

 View Only
Expand all | Collapse all

Manipulating text from an XML file

  • 1.  Manipulating text from an XML file

    Posted 05-26-2014 22:56
    Hello,

    We have an election coming up. The XML file that we will have available to us lists all the ridings, (electoral districts) and several candidates in each riding.

    I was wondering if there was a way to have the 3 candidates who are leading, to display them left to right on the screen, with the leftmost one being the one who so far has the most votes, and if there are 4, 5 or 6 candidates, we will just not show the ones who are not doing well with votes.

    Is there a way to do that with scripting?

    The XML file supplies the candidates in some type of random order - maybe alphabetical, but I was hoping they were sorted by party, but they're not.

    Thanks!


  • 2.  RE: Manipulating text from an XML file

    Posted 05-27-2014 17:01
    I would start by building out one "candidate" group, including everything you were planning on displaying; name, party affiliation, vote count/percentage, photo, etc. Then, duplicate that group so you have "Candidate Group 1," "Candidate Group 2," and "Candidate Group 3."

    If you're displaying and have data for vote count, that will probably be the easiest way to move forward, otherwise, you could use the vote percentage. For the sake of the example, I'll use the vote count, but just substitute it out for percentage if you go the other way.

    Place your groups as placeholders where you would like them to be. You said left to right, so group 1 would be on your left, 3 on your right. Get the X Position value for each of those and write it down. You'll need it later.

    Now, you can get into the scripting part of things. The script I'll help build for you is going to take the vote count from each group, then determine which is the highest, and change the groups' PosX value to match.

    Here's the script:



    'these first three are going to be your candidate groups

    dim candidate1 as xpBaseObject

    dim candidate2 as xpBaseObject

    dim candidate3 as xpBaseObject

    'these three are the vote count values as they come in through datalinq

    dim value1 as xpTextObject

    dim value2 as xpTextObject

    dim value3 as xpTextObject

    'these will be used within the script in order to do math

    dim valint1 as Integer

    dim valint2 as Integer

    dim valint3 as Integer

    'this group is assigning the objects into the script

    Self.GetObjectByName("Candidate Group 1", candidate1)

    Self.GetObjectByName("Candidate Group 2", candidate2)

    Self.GetObjectByName("Candidate Group 3", candidate3)

    Self.GetObjectByName("Votes 1", value1)

    Self.GetObjectByName("Votes 2", value2)

    Self.GetObjectByName("Votes 3", value3)

    'this next block is going to convert the values into integers that the script can do math with

    valint1=Cint(value1.Text)

    valint2=Cint(value2.Text)

    valint3=Cint(value3.Text)

    'and now we plug and chug

    if valint1 > valint2 and valint2 > valint3 then

    candidate1.PosX= 'the value for the left group

    candidate2.PosX= 'the value for the middle group

    candidate3.PosX= 'the value for the right group

    elseif valint1 > valint2 and valint2 < valint3 then

    candidate1.PosX= 'left group

    candidate2.PosX= 'right group

    candidate3.PosX= 'middle group



    and etc., etc., on and on to fill out all possible combinations.

    This is a very messy way of doing it, and someone else can definitely do better, but this would brute force your way through.

    #XPression


  • 3.  RE: Manipulating text from an XML file

    Posted 05-28-2014 17:29
    Thanks!

    I was hoping to avoid messy, I will really have to learn some Visual Basic myself at some point, it's starting to look like.

    One problem is, there is not a set number of candidates per riding, it can range from 6 to 9. (from what I can see from the XML file I've been provided with from a previous election) So I think it would get even messier, if we try to use scripting, to pick the 3 highest "scoring", from possibly 9 candidates. (the remaining candidates are from parties we are not interested in seeing at all) I am thinking of making several templates with the various possibilities (eg. candidates 1, 2, 3, 4 on one page, then candidates 1, 2, 3, 5, etc) and getting around it that way.

    Here's something else I will need help with: the XML feed provides the candidates' names as 2 separate pieces of data. If I were to put one in one text object and one into another, there would be a gap between the 2 names. Can you help me with scripting to put the first initial of the first name with a period, and then the last name, into one text field?

    #XPression


  • 4.  RE: Manipulating text from an XML file

    Posted 05-28-2014 17:49
    It's not impossible, but you're getting pretty crazy, now. I'm in some meetings today, but I'll see if I can hack something together while the kids are in bed tonight. Might be able to figure it out.

    If you would like to do some searching on your own, you're going to be looking for "how to find the highest three numbers of a list using VB" or something like that. Typically, the stuff you'll dig up will be general and not specific to XPression, but it might help out.

    As for learning VB, it's actually quite simple if you can understand how "if, elseif, else, then" statements work. I had no knowledge other than if/then type understanding, and in the last 5 years have been able to adapt some pretty fun stuff. You just need to have an application to use it for, otherwise saying "what can I do with VB?" will only lead to blank-page syndrome of too many options.

    #XPression


  • 5.  RE: Manipulating text from an XML file

    Posted 05-28-2014 18:33
    I just created an example that takes up to 9 candidates and finds the top 3 from the list.

    It also combines First Initial and Last Name..

    Hope you find this useful..

    https://na11.springcm.com/atlas/Link/Document/12442/6ce8e550-96e6-e311-8d7c-d89d67132a6d/ad9a0772-96e6-e311-8d7c-d89d67132a6d

    #XPression


  • 6.  RE: Manipulating text from an XML file

    Posted 05-28-2014 19:54
    Wow Brian, thanks! I was able to see where you were going with that, I had it populate the 9 sets of fields with the XML data, but I wasn't able to see any scripting, it didn't do any sorting for me. Where would the scripting live? Thanks though, it was good for me to see how that was done.

    #XPression


  • 7.  RE: Manipulating text from an XML file

    Posted 05-28-2014 19:55
    The script is in the Scene Object in OnOnline and OnPreviewRender.

    #XPression


  • 8.  RE: Manipulating text from an XML file

    Posted 05-28-2014 19:56
    For anyone that doesn't want to load the whole project, here is the script from OnOnline.

    Sorry the formatting gets lost when posting to the message board; but if you load the xpression project it'll look better organized..



    dim used(20) as boolean ' used for sorting to mark a candidate as already being used

    dim i as integer

    dim j as integer

    dim textobj as xpTextObject

    dim lastname as xpTextObject

    dim firstname as xpTextObject

    dim votesobj as xpTextObject

    ' Clear the used flags

    for i = 0 to 20

    used(i) = false

    next

    dim maxvotes as integer

    dim highestperson as integer

    dim votecount as integer

    'find the three highest people

    for i = 1 to 3

    maxvotes = -9999 ' reset max votes each time

    ' loop over up to 9 candidates finding the highest each time

    for j = 1 to 9

    if used(j) = false then

    if self.GetObjectByName("LastName" & j, lastname) then

    if Len(Trim(lastname.text)) > 0 then ' make sure there is a name

    self.GetObjectByName("Votes" & j, votesobj)

    votecount = CInt(votesobj.text)

    ' does this person have more votes than the previous max

    if votecount > maxvotes then

    maxvotes = votecount

    highestperson = j

    end if

    end if

    end if

    end if

    next

    if maxvotes > 0 then

    used(highestperson) = true ' mark this person as already found..

    self.GetObjectByName("LastName" & highestperson, lastname)

    self.GetObjectByName("FirstName" & highestperson, firstname)

    ' Combine first initial and last name

    self.GetObjectByName("ResultsName" & i, textobj)

    textobj.Text = UCase(firstname.Text(0)) & ". " & lastName.Text

    self.GetObjectByName("Votes" & highestperson, votesobj)

    self.GetObjectByName("ResultsVotes" & i, textobj)

    textobj.Text = votesobj.Text

    end if

    next



    #XPression


  • 9.  RE: Manipulating text from an XML file

    Posted 05-28-2014 20:02
    also when datalinqing, you'd want to make sure to enable the checkbox to "return empty on failure" so if there are less than 9 candidates those extra name/vote fields will get blanked out..

    #XPression


  • 10.  RE: Manipulating text from an XML file

    Posted 05-29-2014 16:27
    When does the script run? I put several instances of the scene in a sequence, and paged through it, and the name fields didn't populate.

    #XPression


  • 11.  RE: Manipulating text from an XML file

    Posted 05-29-2014 17:15
    The script runs whenever the scene goes online, or is previewed.. Here is a video showing the exact xpf I sent earlier and how it works: http://ross.brickftp.com/f/7b02eccce

    Did you make any changes to the project I sent? If you renamed anything then the script might not be able to find an object it is looking for and might just exit..

    Which version of XPression are you running?

    #XPression


  • 12.  RE: Manipulating text from an XML file

    Posted 05-29-2014 18:05
    I didn't rename any fields, I made datalinq connections to the hidden ones so that the XML would feed info into them. I will check which XPression version we have.

    #XPression


  • 13.  RE: Manipulating text from an XML file

    Posted 05-29-2014 18:06
    Did it work before you hooked up the datalinq fields?

    #XPression


  • 14.  RE: Manipulating text from an XML file

    Posted 05-30-2014 18:09
    It was in fact working before I did the datalinq work. I was missing the 2nd percent sign from my "%relid%". It seems to be working better now, I am still working on getting the datalinq hooked up to all the fields.

    We are using XPression Studio v5.15 build 2561.

    #XPression


  • 15.  RE: Manipulating text from an XML file

    Posted 05-30-2014 18:16
    I checked the "return empty on failure" and it is still putting in phantom data where there should be none, eg. when there are fewer than 9 candidates.

    #XPression


  • 16.  RE: Manipulating text from an XML file

    Posted 02-24-2016 01:16
    I am trying to combine @bford 's Election Sorting script and a script to load candidate photo materials from @djensenwrex based on the last name of the candidate in several Candidate mug/results graphics. I have the lastname to photo script running in OnOnline, but when I include it in OnPreviewRender, the online photos will change if I select the item in my Xpression Studio sequence. I plan to publish these templates and play them through one of the Studio outputs via our RossVideo Remote Sequencer. I am waiting on my IT to give me access to the election database, so I have been working toward renaming my layers to work with the Election Sorting script..and expanding it to include vote percentages. I want my producers to be able to enter the race name as the graphic title and I now realize that I may need yet another script to reference the correct place in the database by race name. Any ideas?
    #XPression


  • 17.  RE: Manipulating text from an XML file

    Posted 06-10-2019 17:02

    Is there anywhere to get the files that were associated with this?  They have all timed out.

    Asking because I am trying to do something similar.


    #XPression