0

Here is a related answer to a similar issue:

https://stackoverflow.com/a/19140663/2287576

Variable definition 1:

<xsl:variable name="duty-assignments"
    select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Midweek']/msa:Assignment"/>

Variable definition 2:

<xsl:variable name="duty-assignments"
    select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekly']/msa:Assignment"/>

I can use either of these definitions and my script behaves. But, if I apply the logic of the linked answer:

<xsl:variable name="duty-assignments">
    <xsl:choose>
        <xsl:when test="CircuitVisit='1'>
            <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekly']/msa:Assignment"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Midweek']/msa:Assignment"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

My report stopped rendering. As mentioned it is OK if I drop using the choose statement and only assign one node-set to variable.

Is there some concepts I am overlooking for XSLT1 and this kind of activity?


Additional Information

Here is an extract of some duty assignments:

<?xml version="1.0" encoding="utf-8"?>
<DutyAssignmentHistory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.publictalksoftware.co.uk/msa">
  <DutyAssignments>
    <DutyAssignmentEntry Date="2023-09-21" Week="W20230918" Template="0" Mode="Midweek">
      <Assignment Index="13" IndexType="Fixed">Name 1</Assignment>
      <Assignment Index="14" IndexType="Fixed">Name 2</Assignment>
      <Assignment Index="15" IndexType="Fixed">Name 3</Assignment>
      <Assignment Index="7" IndexType="Fixed">Name 4</Assignment>
      <Assignment Index="8" IndexType="Fixed">Name 5</Assignment>
      <Assignment Index="5" IndexType="Fixed">Name 6</Assignment>
      <Assignment Index="17" IndexType="Fixed">Name 7</Assignment>
    </DutyAssignmentEntry>
    <DutyAssignmentEntry Date="2023-09-24" Week="W20230918" Template="0" Mode="Weekend">
      <Assignment Index="13" IndexType="Fixed">Name 8</Assignment>
      <Assignment Index="14" IndexType="Fixed">Name 9</Assignment>
      <Assignment Index="15" IndexType="Fixed">Name 10</Assignment>
      <Assignment Index="7" IndexType="Fixed">Name 11</Assignment>
      <Assignment Index="8" IndexType="Fixed">Name 12</Assignment>
      <Assignment Index="5" IndexType="Fixed">Name 13</Assignment>
      <Assignment Index="17" IndexType="Fixed">Name 14</Assignment>
    </DutyAssignmentEntry>
    <DutyAssignmentEntry Date="2023-09-26" Week="W20230925" Template="0" Mode="Weekly">
      <Assignment Index="13" IndexType="Fixed">Name 15</Assignment>
      <Assignment Index="14" IndexType="Fixed">Name 1</Assignment>
      <Assignment Index="15" IndexType="Fixed">Name 2</Assignment>
      <Assignment Index="7" IndexType="Fixed">Name 3</Assignment>
      <Assignment Index="8" IndexType="Fixed">Name 4</Assignment>
      <Assignment Index="5" IndexType="Fixed">Name 5</Assignment>
      <Assignment Index="17" IndexType="Fixed">Name 6</Assignment>
    </DutyAssignmentEntry>
    <DutyAssignmentEntry Date="2023-10-01" Week="W20230925" Template="0" Mode="Weekend">
      <Assignment Index="13" IndexType="Fixed">Name 7</Assignment>
      <Assignment Index="14" IndexType="Fixed">Name 8</Assignment>
      <Assignment Index="15" IndexType="Fixed">Name 9</Assignment>
      <Assignment Index="7" IndexType="Fixed">Name 10</Assignment>
      <Assignment Index="8" IndexType="Fixed">Name 11</Assignment>
      <Assignment Index="5" IndexType="Fixed">Name 12</Assignment>
      <Assignment Index="17" IndexType="Fixed">Name 13</Assignment>
    </DutyAssignmentEntry>
  </DutyAssignments>
</DutyAssignmentHistory>

And, here is an example of how I want to use the variable data:

<xsl:value-of select="$duty-assignments[@Index='15' and @IndexType='Fixed']"/>

The function that defines and uses the variable is called like this:

<xsl:call-template name="DutyAssignments"/>

And the function itself:

<!-- Duty assignment template -->
<xsl:template name="DutyAssignments">
</xsl:template>
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Please provide a (short) way to reproduce the problem. The "concept" that you may be missing is that in the first 2 examples the variable is bound to the selected `Assignment` elements/s. In the last example it is bound to the string-value of the first of the selected `Assignment` element/s - not to a node-set as you state. But at this point I am merely guessing. – michael.hor257k Aug 20 '23 at 21:47
  • @michael.hor257k Ah, I think you are on to something. When I output the value it only showed a name. But since that is the case, why does this third example not return nodesets when the queries look the same (to me). I’ll do my best to provide sample data tomorrow if needed. – Andrew Truckle Aug 20 '23 at 21:53
  • 1
    That's what `xsl:value-of` does - see: https://www.w3.org/TR/1999/REC-xslt-19991116/#value-of and https://www.w3.org/TR/1999/REC-xpath-19991116/#function-string. – michael.hor257k Aug 20 '23 at 21:58
  • @michael.hor257k This is related question that uses the same data concept with examples: https://stackoverflow.com/q/48252199/2287576 – Andrew Truckle Aug 20 '23 at 21:58
  • 1
    No, it's not the same. There they are interested in the numeric value of the selected element and nothing else. I don't know what you're trying to do with the variable, but apparently you need more than that. How many `Assignment` elements can there be in your selected node-set? – michael.hor257k Aug 20 '23 at 22:04
  • I am doing the same thing. Basically it came to my attention that when the CircuitVisit is true that I need to use the duty history assignments where the Mode is Weekly. Otherwise I use a mode of Midweek. If I don’t make the conditional adjustment it will not find the right list of assignments for meto access the properties via [x]. Make sense? – Andrew Truckle Aug 20 '23 at 22:07
  • 1
    If your variable needs to contain a **list** of assignments, then you are NOT doing the same thing. Likewise if your variable needs to hold any other property of the selected assignments other than the string-value of the first one. – michael.hor257k Aug 20 '23 at 22:11
  • @michael.hor257k you are right. Although the duty history is exactly the same as the linked question I will flesh this one out tomorrow. I can clearly see that using value-of to assign the variable is not ok. When I wanted to get all the assignments for that duty week. Much like the other question was about midweek v weekend. Then there is the third choice of weekly. – Andrew Truckle Aug 20 '23 at 22:16
  • 1
    You *may* be able to use `xsl:copy-of` instead of `xsl:value-of`. However, that has a set of problems of its own - see: https://stackoverflow.com/a/73920360/3016153. Eventually, if you really need to select between 2 or more node-sets when binding a variable, you need to use the technique shown here: https://stackoverflow.com/a/57803763/3016153. Again, much depends on what you're actually doing with the variable. Perhaps you can use a completely different method to accomplish your goal - e.g. modes. – michael.hor257k Aug 20 '23 at 22:29
  • @michael.hor257k I getcha. Use conditional | so that it picks one or the other. I’ll try tomorrow and update this question. My code is run in a call template and not an apply template. So modes are not allowed. – Andrew Truckle Aug 20 '23 at 22:33
  • Morning @michael.hor257k I fleshed out the question and I can confirm that if I change the variable to use that `|` syntax that it all works correctly. – Andrew Truckle Aug 21 '23 at 07:21

0 Answers0