To be clear, I have already found a way of accomplishing what I wanted to using XSLT, but it strikes me as largely inefficient and I would like to see if it is possible to use a different solution because it would aid in writing future stylesheets.
Also, I sincerely apologize for the following paragraph's verbosity.
Minimally, I am trying to extract from a story written in Russian and encoded in XML ('pavlova.xml' - unfortunately way too large of a file to post with my question but it isn't completely necessary) data which would be useful for generating a pie chart in SVG. I'm trying to store this data in a variable called $chartData. A given pie chart will pertain to a target character (a parameter) overall, and each separate chunk of the pie will represent a speaker (another character), their sizes indicating how much each speaker spoke of the target character relative to other speakers. I began by iterating over all of the speakers (any character that ever mentioned the target character) and then determining the quantity of times the character's name showed up in the particular speaker's speech. However, these values are only useful in relation to the sum total of times that the character was spoken of. I need the total in order to calculate percentages to generate the pie graph. I am aware that I am able to separately calculate this in another variable, but I am interested in seeing if I can store a sequence of nodes in a variable. Calculating the sum separately seems wasteful since it traverses a path nearly identical to the one that finds the individual counts, where it would be much more efficient to just sum the individual values after they are retrieved. So far, I have been able to calculate everything I want, but I am trying instead to iterate over a variable using an xsl:for-each and have it recognize my variable as a sequence.
I want the format of the variable to be this:
<count name="$speaker1>
<xsl:value-of select="$spokenCount1"/>
</count>
<count name="$speaker2>
<xsl:value-of select="$spokenCount2"/>
</count>
...
I've tried many ways of solving this issue that I'm not sure is possible to solve. First was more accurately defining the content of the xsl:variable (specifically, trying to define its content as a sequence) by using the @as attribute, but I'm finding it difficult to locate and comprehend documentation on that attribute and how SequenceType's work. Secondly, I tried playing around with how I put the content into my variable (directly putting the for-each within the variable and either using xsl:value-of or xsl:copy-of, versus declaring the variable and using xsl:copy-of in an external for-each to append data to the variable).
It seems that xsl:copy-of, as opposed to xsl:sequence (ironically) is what I need to append a sequence of nodes. I was only able to obtain the format I described above with the following code:
<xsl:variable name="chartData"/>
<xsl:for-each
select="distinct-values(//speech[.//name[not(parent::nonspeech) and ./@ref eq $character]]/@speaker)">
<xsl:copy-of select="$chartData"/>
<xsl:variable name="speaker" select="current()"/>
<xsl:variable name="spokenCount"
select="document('pavlova.xml')/count(//speech[@speaker eq $speaker]//name[not(parent::nonspeech) and ./@ref eq $character])"/>
<count name="{$speaker}">
<xsl:value-of select="$spokenCount"/>
</count>
</xsl:for-each>
<xsl:variable name="spokenTotal" select="sum($chartData)"/>
<xsl:value-of select="$chartData"/>
<!--xsl:for-each select="$chartData/count">
...
</xsl:for-each-->
The end goal is to make the commented out for-each loop at the bottom iterate over each count element instead of giving me an error. My question is, can I treat a variable as XML and iterate over each count element if it looks like the following, or is it always treated as a string?
<?xml version="1.0" encoding="UTF-8"?>
<count name="young-man">4</count>
<count name="olga">24</count>
<count name="cecilija">34</count>
<count name="blonde">1</count>
<count name="vera">32</count>
<count name="servant-fem">6</count>
<count name="valickaja">20</count>
<count name="muse">66</count>
<count name="viktor">4</count>
<count name="dmitrij">15</count>
<count name="anna">11</count>
<count name="iličev">3</count>
<count name="narod">1</count>
<count name="society-man">3</count>
And if so, how?
Also, if someone could explain, why does xsl:copy-of only deep-copy when I have the for-each outside of the variable's scope and set select to '$chartData'? If I try to use xsl:copy-of within the variable it only copies the text content of the literal <count>
elements and creates a string: 424341326206641511313).