0

I am trying to filter through my XML using XSLT. I have a list of programming standards, and I am trying to figure out how to display all the languages that have that specific standard in a table data cell. So far, I have got it displaying all languages in each cell. My logic is off. Ideas?

XML

    <?xml version="1.0" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="standards_XSL.xsl"?>

<programming>
  <languages>
    <language id="1">
      <name>C</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="2" />
        <intendedUse id="3" />
        <intendedUse id="4" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="4" />
      </features>
      <standards>
        <standard id="1" />
        <standard id="2" />
      </standards>
    </language>
    <language id="2">
      <name>C++</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="2" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="3" />
        <feature id="4" />
        <feature id="5" />
      </features>
      <standards>
        <standard id="2" />
      </standards>
    </language>
    <language id="3">
      <name>C#</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="5" />
        <intendedUse id="6" />
        <intendedUse id="7" />
        <intendedUse id="3" />
        <intendedUse id="8" />
        <intendedUse id="9" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="3" />
        <feature id="4" />
        <feature id="5" />
        <feature id="6" />
        <feature id="7" />
      </features>
      <standards>
        <standard id="3" />
        <standard id="2" />
      </standards>
    </language>
    <language id="4">
      <name>Java</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="6" />
        <intendedUse id="7" />
        <intendedUse id="3" />
        <intendedUse id="8" />
        <intendedUse id="9" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="5" />
        <feature id="6" />
      </features>
      <standards>
        <standard id="4" />
      </standards>
    </language>
    <language id="5">
      <name>JavaScript</name>
      <purpose>
        <intendedUse id="7" />
        <intendedUse id="8" />
        <intendedUse id="7" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="3" />
        <feature id="6" />
      </features>
      <standards>
        <standard id="3" />
      </standards>
    </language>
    <language id="6">
      <name>PHP</name>
      <purpose>
        <intendedUse id="8" />
        <intendedUse id="9" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="4" />
        <feature id="6" />
      </features>
      <standards>
        <standard id="5" />
      </standards>
    </language>
    <language id="7">
      <name>Python</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="3" />
        <intendedUse id="9" />
        <intendedUse id="10" />
        <intendedUse id="11" />
        <intendedUse id="12" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="3" />
        <feature id="6" />
      </features>
      <standards>
        <standard id="5" />
      </standards>
    </language>
    <language id="8">
      <name>Basic</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="13" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="4" />
      </features>
      <standards>
        <standard id="1" />
        <standard id="2" />
      </standards>
    </language>
    <language id="9">
      <name>ADA</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="2" />
        <intendedUse id="14" />
        <intendedUse id="15" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="4" />
        <feature id="5" />
      </features>
      <standards>
        <standard id="1" />
        <standard id="2" />
        <standard id="6" />
      </standards>
    </language>
    <language id="10">
      <name>ActionScript 3.0</name>
      <purpose>
        <intendedUse id="1" />
        <intendedUse id="7" />
        <intendedUse id="9" />
      </purpose>
      <features>
        <feature id="1" />
        <feature id="2" />
        <feature id="7" />
      </features>
      <standards>
        <standard id="3" />
      </standards>
    </language>
  </languages>

  <purpose>
    <intendedUse id="1"> Application</intendedUse>
    <intendedUse id="2"> System </intendedUse>
    <intendedUse id="3"> General Purpose </intendedUse>
    <intendedUse id="4"> Low-Level Operations </intendedUse>
    <intendedUse id="5"> RAD </intendedUse>
    <intendedUse id="6"> Business </intendedUse>
    <intendedUse id="7"> Client-Side </intendedUse>
    <intendedUse id="8"> Server-Side </intendedUse>
    <intendedUse id="9"> Web </intendedUse>
    <intendedUse id="10"> Scripting </intendedUse>
    <intendedUse id="11"> AI </intendedUse>
    <intendedUse id="12"> Scientific Computing </intendedUse>
    <intendedUse id="13">Education</intendedUse>
    <intendedUse id="14">Embedded</intendedUse>
    <intendedUse id="15">RealTime</intendedUse>
  </purpose>
  <features>
    <feature id="1">Imperative </feature>
    <feature id="2">Object-Oriented</feature>
    <feature id="3">Functional</feature>
    <feature id="4">Procedural</feature>
    <feature id="5">Generic</feature>
    <feature id="6">Reflective</feature>
    <feature id="7">Event-Driven</feature>
  </features>
  <standards>
    <standard id="1">ANSI</standard>
    <standard id="2">ISO</standard>
    <standard id="3">ECMA</standard>
    <standard id="4">DE FACTO</standard>
    <standard id="5">NO</standard>
    <standard id="6">GOST</standard>
  </standards>
</programming>

UPDATE XSLT

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="language-by-standard" match="language" use="standards/standard/@id" />
   <xsl:param name="selectedStandard">ECMA</xsl:param>


<xsl:template match="/" >
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Standards</th>
                    <th>Language</th>
                </tr>
                <xsl:for-each select="programming/standards[standard=$selectedStandard]">
                    <tr>
                        <td>
                            <xsl:value-of select="$selectedStandard"/>
                        </td>
                        <td>
                            <xsl:for-each select="key('language-by-standard', /programming/standards/standard/@id)" >
                                <xsl:value-of select="name" />
                                <br />
                            </xsl:for-each>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>
1
  • 1. Please cut short your input to what is enough to describe the problem. 2. Please post the expected output for that input. Commented Mar 23, 2015 at 3:57

1 Answer 1

1

Your XML uses IDs to link languages to standards. XSLT has a built-in key mechanism to handle inner links and perform a lookup.

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="language-by-standard" match="language" use="standards/standard/@id" />

<xsl:template match="/" >
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Standards</th>
                    <th>Language</th>
                </tr>
                <xsl:for-each select="programming/standards/standard">
                    <tr>
                        <td>
                            <xsl:value-of select="."/>
                        </td>
                        <td>
                            <xsl:for-each select="key('language-by-standard', @id)" >
                                <xsl:value-of select="name" />
                                <br />
                            </xsl:for-each>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

Result (rendered):

enter image description here

Or, if you prefer:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="language-by-standard" match="language" use="standards/standard/@id" />

<xsl:template match="/" >
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Standards</th>
                    <th>Language</th>
                </tr>
                <xsl:for-each select="programming/standards/standard">
                    <xsl:variable name="languages" select="key('language-by-standard', @id)" />
                    <tr>
                        <td rowspan="{count($languages)}">
                            <xsl:value-of select="."/>
                        </td>
                        <td>
                            <xsl:value-of select="$languages[1]/name" />
                        </td>
                    </tr>
                    <xsl:for-each select="$languages[position() > 1]" >
                        <tr>
                            <td>
                                <xsl:value-of select="name" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

to produce:

enter image description here

which may be more useful if you plan to expand the table further to the right.


Edit:

In response to your update: if you only want to see the languages for the selected standard passed as a parameter, then there will be only one row in the Standards column - so that:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="selectedStandard">ECMA</xsl:param>
<xsl:key name="language-by-standard" match="language" use="standards/standard/@id" />

<xsl:template match="/" >
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Standard</th>
                    <th>Languages</th>
                </tr>
                <tr>
                    <td>
                        <xsl:value-of select="$selectedStandard"/>
                    </td>
                    <td>
                        <xsl:for-each select="key('language-by-standard', programming/standards/standard[.=$selectedStandard]/@id)" >
                            <xsl:value-of select="name" />
                            <br />
                        </xsl:for-each>
                    </td>
                </tr>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

will return:

<html>
   <body>
      <table border="1">
         <tr>
            <th>Standard</th>
            <th>Languages</th>
         </tr>
         <tr>
            <td>ECMA</td>
            <td>C#<br>JavaScript<br>ActionScript 3.0<br></td>
         </tr>
      </table>
   </body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Now, i want to try and filter with parameters. how can i implement this? I am using parameters. @michael.hor257k I have the selected standard showing, but when it comes to filtering the langauges, it doesnt display anything unless i manually input the id. I will add what i have so far and my output.
It worked! sorry, i did repost the same updated question only because i didnt see your edit, and i been stuck for a few hours. I feel dumbfounded that i just needed a period. (as I was on the right track, I had standard = $selectedLanguge. Thanks Again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.