我尝试了几件东西......使用变式和模板,并取得了一些缓慢的进展。 但是,我不要忘记这一点——与我的情况相左。
投入......
- 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.
我质疑我的守则,而以下做法可能并非完全有效,而是对我所尝试的内容的看法。
我的第一个问题是——我是否走上正轨?
- Pass Subject to Grades template
- Capture the index of Subject
- Pass Subject and Index to add-Grades-nodes template
这正是我的背景问题所阻止的。
纽约总部 我的投入
<?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>
- 希望产出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>
————————
<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>