I’ve tried several things… using Variables and Templates, and have made some slow progress. But I’m just not getting this to work – having troubles with my Context.
The input…
- Table ‘700’ can contain multiple entries.
- The values for Subject1, Subject2, Subject3 will be English, Math, Science (But the order may vary from one source XML to another)
- The Scores are positionally ties to the Subject (That is Score1 is for Subject1)
The OutPut…
- Will ALWAYS contain 6 nodes (ENGLISH, MATH, SCIENCE, Class, Class2, Class3)
- The Subject_ tags order ALWAYS needs to be English, Math, then Science.
- The Subject_ tags will be upper cased
- The Subject_ tags will contain a flag of 1 if the corresponding score is > 0; otherwise 0
- The Class_Score tags order order ALWAYS needs to be English, Math, then Science.
I’ve mangled my code and the following may not be completely functional, but gives a view into what I’ve tried.
My first question is – am I on the right track?
- Pass Subject to Grades template
- Capture the ‘index’ of Subject
- Pass Subject and Index to add-Grades-nodes template
This is where my Context issue stops me.
— My input XML —
<?xml version="1.0"?>
<Account Number="123456">
<Data>
<Table ID="700">
<Record ID="1" SubClass="Person">
<Name.Last>Smith</Name.Last>
<Name.First>John</Name.First>
<Score1>50</Score1>
<Score2>75</Score2>
<Score3>100</Score3>
<Subject1>Math</Subject1>
<Subject2>English</Subject2>
<Subject3>Science</Subject3>
</Record>
<Record ID="2" SubClass="Person">
<Name.Last>Smith</Name.Last>
<Name.First>Jane</Name.First>
<Score1></Score1>
<Score2>77</Score2>
<Score3>80</Score3>
<Subject1>Math</Subject1>
<Subject2>English</Subject2>
<Subject3>Science</Subject3>
</Record>
</Table>
</Data>
</Account>
— Desired output XML —
<Out>
<Subject_ENGLISH>1</Subject_ENGLISH>
<Subject_MATH>1</Subject_MATH>
<Subject_SCIENCE>1</Subject_SCIENCE>
<Class_SCORE>75</Class_SCORE>
<Class2_SCORE>50</Class2_SCORE>
<Class3_SCORE>100</Class3_SCORE>
<Subject_ENGLISH>0</Subject_ENGLISH>
<Subject_MATH>1</Subject_MATH>
<Subject_SCIENCE>1</Subject_SCIENCE>
<Class_SCORE></Class_SCORE>
<Class2_SCORE>77</Class2_SCORE>
<Class3_SCORE>80</Class3_SCORE>
</Out>
— Current XSLT that does not work —
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="Account">
<Out>
<xsl:for-each select="/Account/Data/Table[@ID='700']/Record[@SubClass='Person']>
<xsl:call-template name="Grades">
<xsl:with-param name="Subject">English</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="Grades">
<xsl:with-param name="Subject">Math</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="Grades">
<xsl:with-param name="Subject">Science</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</Out>
</xsl:template>
<xsl:template name="Grades">
<xsl:param name="Subject"/>
<xsl:for-each select="*[starts-with(name(), 'Subject')][node()=$Subject]">
<xsl:variable name='cr-index'>
<xsl:value-of select ='substring(name(), string-length(name()))'/>
</xsl:variable>
<xsl:call-template name="create-Grades-nodes">
<xsl:with-param name="cr-context" select =".."/>
<xsl:with-param name="Subject">
<xsl:value-of select='$Subject' />
</xsl:with-param>
<xsl:with-param name="cr-index">
<xsl:value-of select='$cr-index'/>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="create-Grades-nodes" match="$cr-context" >
<xsl:param name ="cr-context"/>
<xsl:param name ="Subject"/>
<xsl:param name ="cr-index"/>
<xsl:variable name='cr-score'>
<xsl:value-of select='name($cr-context)/concat("Score", $cr-index)'/>
</xsl:variable>
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:element name='{concat("Subject_", translate($Subject, $smallcase, $uppercase))}'>
<xsl:choose>
<xsl:when test= '$cr-score > 0'>
<xsl:value-of select = "1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select = "0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This transformation:
when applied on the provided XML document:
produces the wanted, correct result: