XSL Getting every node instead of just one?

Having trouble installing <oXygen/>? Got a bug to report? Post it all here.
Posts: 3

XSL Getting every node instead of just one?

Fri Feb 03, 2017 1:33 am

Okay, I'm a newbie as well, so please forgive.

Here is my XML file:
<name>Writing, Journalism &amp; Literature</name>
<category-description/><program-area code="PA0073">
<name>Comparative &amp; English Literature</name>
<program-area-description/><program-stream code="PS0199">

Here is my XSL
<xsl:element name="ParaStyle--HeadList2">
<xsl:variable name="title3" select="program-area/name"/>
<xsl:value-of select="$title3"/>

Here is my output:
<ParaStyle--HeadList1>Writing, Journalism &amp; Literature</ParaStyle--HeadList1>
<ParaStyle--HeadList2>Comparative &amp; English Literature Journalism Writers' Program/Fiction Writers' Program/Creative Nonfiction Writers' Program/Playwriting, Poetry, Youth Market Writing Writers' Program/Feature Film Writers' Program/Television</ParaStyle--HeadList2>

Here is what I want:
<ParaStyle--HeadList1>Writing, Journalism &amp; Literature</ParaStyle--HeadList1>
<ParaStyle--HeadList2>Comparative &amp; English Literature</ParaStyle--HeadList2>

How can I tell it not to grab ALL the <names></names>? I only want the "current" one.

Thank you!
Posts: 2460

Re: XSL Getting every node instead of just one?

Fri Feb 03, 2017 11:52 am


I can't tell the structure of your XML document from the example you provided. Also, your XSL snippet doesn't have a context (template with a match).
Please provide proper XML and XSL samples, a well formed XML (all tags correctly closed) snippet with a single item and a full xsl:template.

Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
Posts: 3

Re: XSL Getting every node instead of just one?

Fri Feb 03, 2017 9:12 pm

I'm not sure how much of the XML you need - it's 50,000 lines long and my first XSL project! Here is some more of it.

Code: Select all

      <name>Writing, Journalism &amp; Literature</name>
      <category-description />
      <program-area code="PA0073">
         <name>Comparative &amp; English Literature</name>
         <program-area-description />
         <program-stream code="PS0199">
            <program-stream-name>Comparative Literature</program-stream-name>
            <program-stream-description>&amp;lt;p&amp;gt;&amp;lt;em&amp;gt;For more information call (310) 825-7093.&amp;lt;/em&amp;gt;&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;See also Literature, page 159.&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;</program-stream-description>
               <program-office>Humanities and Sciences</program-office>
               <course-number>COM LIT XL 1C</course-number>
                  <new-course-title />
                  <revised-course-title />
               <title>World Literature: Age of Enlightenment to 20th Century</title>
                  <former-title>World Literature: Age of Enlightenment to the 20th Century</former-title>
                  <credit-title>Credit course</credit-title>
               <x-number />
               <discipline-name>COM LIT</discipline-name>
                  <academic-unit-unit />
                  <continuing-education-unit-value />
                  <continuing-education-unit-unit />
               <professional-associations />
               <description>Literature since the European 18th century is marked by fundamental changes in our thinking about what it means to live in this world as rational beings. We remain heirs of that &amp;quot;enlightened&amp;quot; tradition in which humans, in their glory and darkness, are at the center of our focus. This course examines some masterworks of the Western tradition from the Enlightenment: Pope&amp;#39;s Essay on Man, Voltaire&amp;#39;s &amp;lt;em&amp;gt;Candide&amp;lt;/em&amp;gt;, and Goethe&amp;#39;s &amp;lt;em&amp;gt;Faust&amp;lt;/em&amp;gt;, as well as 19th- and 20th-century literature, including poetry of the French symbolists, Emily Dickinson, and T. S. Eliot; works by Tolstoy, Ibsen, Woolf, Pirandello, and Kafka; selections from Sigmund Freud; and the literary postmodernism of Jorge Luis Borges and Alain Robbe-Grillet.Transferable for UC credit.&amp;nbsp;</description>
               <catalog-description />
                  <outcomes-title>Topics include:</outcomes-title>
                  <outcome />
                  <note>&amp;lt;b&amp;gt;&amp;lt;u&amp;gt;Contact:&amp;lt;/u&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;
Jennifer Polutan&amp;lt;br /&amp;gt;
(310) 825-7093&amp;lt;br /&amp;gt;
                  <required-prerequisite>Satisfaction of Subject A requirement, or consent of instructor.</required-prerequisite>
                  <recommended-prerequisite />
                  <course-instructor instructor-number="T000875">
                     <instructor-name>Leonard Koff</instructor-name>
               <course-fees />
               <catalog-note />
                     <section-title />
                     <section-number>COM LIT XL 1C-008</section-number>
                     <section-campus-id />
                     <section-notes>&amp;lt;p&amp;gt;Applies toward the Literary and Cultural Analysis requirement under the Foundation of Arts and Humanities.&amp;lt;br /&amp;gt;
Midterm and final exams are proctored online; additional requirements include microphone, headphones/speakers, and webcam.&amp;lt;br /&amp;gt;
Enrollment limited to 25 students.&amp;lt;br /&amp;gt;
Enrollment deadline: April 12, 2017.&amp;nbsp;&amp;lt;br /&amp;gt;
No refund after April 12, 2017. Materials required. Internet access required.&amp;nbsp;&amp;lt;/p&amp;gt;</section-notes>
                        <required-texts-title>Required Text(s):</required-texts-title>
                        <required-text />
                        <recommended-texts-title>Recommended Text(s):</recommended-texts-title>
                        <recommended-text />
                        <instructors-title />
                        <instructor-list instructor-number="T000875">
                           <instructor>Leonard Koff</instructor>
                           <instructor-biography>PhD, UC Berkeley; associate, UCLA Center for Medieval and Renaissance Studies.</instructor-biography>

Here is the xsl:template directly above the code I wrote (this project was started and abandoned by someone else).

Code: Select all

  <xsl:template match="category">
        <xsl:element name="category">
            <xsl:element name="ParaStyle--HeadList1">
                <xsl:variable name="title1"  select="name"/>
                <!--<xsl:value-of select="name"/>-->                                   
                <xsl:value-of select="$title1"/>
            <!--Carriage Return-->
            <!--Get program-area/name-->
            <xsl:element name="ParaStyle--HeadList2">
                <!--DNW <xsl:variable name="title3"  select="program-area/name[1]"/>    -->
                <!--<xsl:variable name="title3"  select="program-area/name"/>-->
                <!--<xsl:variable name="title3"  select="/catalog/category/program-area/name"/>-->
                <!--<xsl:variable name="title3"  select="/catalog/category/program-area/name[1]"/>-->
                <xsl:variable name="title3"  select="program-area/name"/>
                <!--<xsl:variable name="title3"  select="program-area/name."/>-->
                <!--<xsl:variable name="title3"  select="program-area/name"/> -->
                <!--EMPTY <xsl:variable name="title3"  select="/program-area/name"/> -->
                <!--<xsl:variable name="title3"  select="program-area/name"/>-->
                <!--<xsl:variable name="title3"  select="program-area/name[0]"/>-->
                <xsl:value-of select="$title3"/>               
            <xsl:element name="ParaStyle--BodyText_Space">
                <xsl:variable name="title4"  select="/catalog/category/program-area/program-stream/program-stream-description"/>               
                <!--<xsl:value-of select="name"/>-->
                <!--<xsl:variable name="title3" select="translate($title3,'&amp;','0x0026')"/>-->
                <!--<xsl:variable name="title3" select="replace($title3,'&amp;','0x0026')"/>-->
                <xsl:value-of select="$title4"/>               
            <xsl:apply-templates select="program-area/program-stream/course"/>

Thank you!
Posts: 5866

Re: XSL Getting every node instead of just one?

Mon Feb 06, 2017 5:18 pm


You should try using our XSLT debugger, add some breakpoints and look at various variable values:


A couple of things to take into account, as you have an XSLT template which matches "category", all your XPaths which do not start with "/" will be relative to that category node. XPath's starting with "/" will be absolute, starting from the root element down.

Radu Coravu
<oXygen/> XML Editor
Posts: 3

Re: XSL Getting every node instead of just one?

Wed Feb 08, 2017 9:12 pm

What "various variables"? I've only got the one "title3"?
Posts: 5866

Re: XSL Getting every node instead of just one?

Thu Feb 09, 2017 9:27 am


I just wanted to express the fact that you need to start helping yourself and the XSLT debugger is a good way to understand how the XSLT stylesheet is applied, what XML nodes are matched in a certain context and so on.

Radu Coravu
<oXygen/> XML Editor

Return to “Common Problems”

Who is online

Users browsing this forum: No registered users and 3 guests