Andrew Truckle
Andrew Truckle

Reputation: 19117

Combining text values together using XSL

I have a XML file. Snippet:

<?xml version="1.0" encoding="UTF-8"?>

<AssignmentHistory>
<W20160104>
    <StudentItems>
        <Item>
            <Name Counsel="13">Name 1</Name>
            <Type>Type 1</Type>
        </Item>
    </StudentItems>
</W20160104>

I am wanting to create a XSL script that primarily works with another XML file. However, it also refers to the above external XML file using this method:

<xsl:variable name="MyXML" select "document('history.xml')"/>

In the main XML file, I am in a 'Meeting' node which has this content:

<MeetingDate Day="7" DayShort="Thu" DayFull="Thursday" Month="1" MonthShort="Jan" MonthFull="January" Year="2016"/>

What I want to do, is combine the @Day, @Month and @Year attributes so that I end up with "WYYYYMMDD" so that I can then hopefully locate:

/AssignmentHistory/WYYYYMMDD

So that I can then include some values from that node in MyXML in my final output.

Can you please advise me how to take the attributes and convert them into "WYYYYMMDD"?

Thank you very much.

Thanks to your comments I made some progress. I can now format the WYYYYMMDD text value. But I am struggling to iterate the external document:

            <xsl:variable name="AssignHistory" select="document('AssignHistory.xml')"/>
            <xsl:variable name="y" select="MeetingDate/@Year"/>
            <xsl:variable name="m" select="format-number(MeetingDate/@Month, '00')"/>
            <xsl:variable name="d" select="format-number(MeetingDate/@Day, '00')"/>
            <xsl:variable name="week" select="concat('W',$y,$m,$d)"/>
            <xsl:value-of select="$week"/><br />
            <xsl:for-each select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems/Item">
              Student (or assistant): <xsl:value-of select="Name"/><br />
            </xsl:for-each>

Nothing is showing.

Upvotes: 0

Views: 31

Answers (1)

har07
har07

Reputation: 89295

This is one possible way to produce string in the desired format "WYYYMMDD" :

<xsl:template match="MeetingDate">
    <xsl:variable name="y" select="@Year"/>
    <xsl:variable name="m" select="format-number(@Month, '00')"/>
    <xsl:variable name="d" select="format-number(@Day, '00')"/>
    <xsl:variable name="name" select="concat('W',$y,$m,$d)"/>
    <xsl:value-of select="$name"/>
</xsl:template>

xsltranfsorm.net demo

output :

W20160107

xsl:variables are used above to split the expression into chunks to make it easier to read. The actual usage for selecting target element from the XML document variable should look about like this :

<xsl:value-of select="$MyXML/AssignmentHistory/*[name()=$name]"/>

Upvotes: 1

Related Questions