Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:51:45+00:00 2026-05-28T02:51:45+00:00

I’ve tried several things… using Variables and Templates, and have made some slow progress.

  • 0

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 &gt; 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:51:45+00:00Added an answer on May 28, 2026 at 2:51 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pSubjects">
      <s name="English" at="1"/>
      <s name="Math"    at="2"/>
      <s name="Science" at="3"/>
     </xsl:param>
    
     <xsl:variable name="vSubjects" select=
     "document('')/*/xsl:param[@name='pSubjects']/*"/>
    
     <xsl:variable name="vLower" select=
      "'abcdefghijklmnopqrstuvwxyz'"/>
    
     <xsl:variable name="vUpper" select=
      "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    
     <xsl:template match="/*">
      <Out>
       <xsl:apply-templates/>
      </Out>
     </xsl:template>
    
     <xsl:template match="Table[@ID='700']/Record">
      <xsl:apply-templates select="*[starts-with(name(), 'Su')]">
        <xsl:sort select="$vSubjects[@name = current()]/@at" data-type="number"/>
      </xsl:apply-templates>
    
      <xsl:apply-templates select="*[starts-with(name(), 'Sc')]">
       <xsl:sort select=
        "$vSubjects[@name
                   = current()/../*[starts-with(name(), 'Su')]
                        [substring-after(name(), 'Subject')
                        =
                         substring-after(name(current()), 'Score')
                        ]
                    ]/@at"
         data-type="number"/>
      </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="*[starts-with(name(), 'Su')]">
       <xsl:variable name="vgenName" select=
       "concat('Subject_',
                translate(., $vLower, $vUpper)
               )"/>
      <xsl:element name="{$vgenName}">
        <xsl:value-of select="."/>
      </xsl:element>
     </xsl:template>
    
     <xsl:template match="*[starts-with(name(), 'Sc')]">
      <xsl:variable name="vInd" select=
       "$vSubjects[@name
                   = current()/../*[starts-with(name(), 'Su')]
                        [substring-after(name(), 'Subject')
                        =
                         substring-after(name(current()), 'Score')
                        ]
                    ]/@at"/>
    
      <xsl:variable name="vgenName" select=
       "concat('Class',
               translate($vInd, '1', ''),
               '_SCORE'
               )
       "/>
       <xsl:element name="{$vgenName}">
        <xsl:value-of select="."/>
       </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <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>
    

    produces the wanted, correct result:

    <Out>
      <Subject_ENGLISH>English</Subject_ENGLISH>
      <Subject_MATH>Math</Subject_MATH>
      <Subject_SCIENCE>Science</Subject_SCIENCE>
      <Class_SCORE>75</Class_SCORE>
      <Class2_SCORE>50</Class2_SCORE>
      <Class3_SCORE>100</Class3_SCORE>
      <Subject_ENGLISH>English</Subject_ENGLISH>
      <Subject_MATH>Math</Subject_MATH>
      <Subject_SCIENCE>Science</Subject_SCIENCE>
      <Class_SCORE>77</Class_SCORE>
      <Class2_SCORE></Class2_SCORE>
      <Class3_SCORE>80</Class3_SCORE>
    </Out>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.