0

I have reviewed the questions that may already have your answer section, and the solutions did not work.

I am pretty new to XML stylesheets, I have an XSL and within my for each loop, the data keeps repeating, when I am trying to get all the text within the tag. It has the count (amount of values within the parent tag) accurate, but only prints the same text.

Any ideas?

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

     <xsl:template match="/programming/languages/language">

        <h3>=======Programming Language <xsl:value-of select="./@id" />=======</h3>
        <p>Language: <xsl:value-of select="name" /></p>

        Intended Use:
        <xsl:variable name="intendedUseID" select="purpose/intendedUse/@id" />
        <xsl:for-each select="/programming/purpose/intendedUse[@id=$intendedUseID]" >
           <xsl:value-of select="/programming/purpose/intendedUse" />
        </xsl:for-each>

        <br />

        <xsl:variable name="featureID" select="features/feature/@id" />
             <xsl:for-each select="/programming/features/feature[@id=$featureID]">
                <xsl:value-of select="/programming/features/feature" /><br />
            </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>

XML

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

This is what prints out

=======Programming Language 1=======

Language: C

Intended Use: Application Application Application Application Imperative Imperative

=======Programming Language 2=======

Language: C++

Intended Use: Application Application

Imperative Imperative Imperative Imperative Imperative

Onlytito
  • 167
  • 1
  • 2
  • 17

1 Answers1

1

First thing:

<xsl:for-each select="/programming/purpose/intendedUse[@id=$intendedUseID]" >

puts you in the context of intendedUse. From this context, you just need to grab the value of the current node by doing:

<xsl:value-of select="." />

The way you have it:

<xsl:value-of select="/programming/purpose/intendedUse" />

goes back to the root and selects the first node on this path.

The second thing is that when your first and only template starts in the middle of the input tree, the higher-up nodes and their descendants will be handled by the built-in template rules.

As a result, you will accumulate a lot of garbage text in the result because by default, all text nodes are copied to the output tree. You should make your first template match the root node:

<xsl:template match="/">

and change the included paths to start from there.

Finally, you should consider using keys to lookup data from other branches of the document: it's neater and more efficient, too.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51