XSL Getting every node instead of just one?

Having trouble installing <oXygen/>? Got a bug to report? Post it all here.
cauldronofevil
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"/>
</xsl:element>

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!
adrian
Posts: 2449

Re: XSL Getting every node instead of just one?

Fri Feb 03, 2017 11:52 am

Hi,

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.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
cauldronofevil
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

<category>
      <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>
            <course>
               <program-office>Humanities and Sciences</program-office>
               <course-number>COM LIT XL 1C</course-number>
               <new-course>
                  <new-course-title />
               </new-course>
               <revised-course>
                  <revised-course-title />
               </revised-course>
               <title>World Literature: Age of Enlightenment to 20th Century</title>
               <former-titles>
                  <former-title-title>Formerly</former-title-title>
                  <former-title>World Literature: Age of Enlightenment to the 20th Century</former-title>
               </former-titles>
               <credit>
                  <credit-title>Credit course</credit-title>
               </credit>
               <x-number />
               <discipline-name>COM LIT</discipline-name>
               <academic-units>
                  <academic-unit-value>5.0</academic-unit-value>
                  <academic-unit-unit />
               </academic-units>
               <continuing-education-units>
                  <continuing-education-unit-value />
                  <continuing-education-unit-unit />
               </continuing-education-units>
               <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>
                  <outcomes-title>Topics include:</outcomes-title>
                  <outcome />
               </outcomes>
               <notes>
                  <notes-title>Note(s):</notes-title>
                  <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;
jpolutan@unex.ucla.edu</note>
               </notes>
               <required-prerequisites>
                  <required-prerequisites-title>Prerequisite(s):</required-prerequisites-title>
                  <required-prerequisite>Satisfaction of Subject A requirement, or consent of instructor.</required-prerequisite>
               </required-prerequisites>
               <recommended-prerequisites>
                  <recommended-prerequisites-title>Recommended:</recommended-prerequisites-title>
                  <recommended-prerequisite />
               </recommended-prerequisites>
               <course-instructors>
                  <course-instructor instructor-number="T000875">
                     <instructor-status>final_approval</instructor-status>
                     <instructor-name>Leonard Koff</instructor-name>
                  </course-instructor>
               </course-instructors>
               <course-fees />
               <catalog-note />
               <course-hours>42</course-hours>
               <sections>
                  <section>
                     <section-title />
                     <section-number>COM LIT XL 1C-008</section-number>
                     <custom-section-number>352389</custom-section-number>
                     <section-campus-id />
                     <section-is-distance>1</section-is-distance>
                     <section-notes-label>Notes:</section-notes-label>
                     <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>
                        <required-texts-title>Required Text(s):</required-texts-title>
                        <required-text />
                     </required-texts>
                     <recommended-texts>
                        <recommended-texts-title>Recommended Text(s):</recommended-texts-title>
                        <recommended-text />
                     </recommended-texts>
                     <instructors>
                        <instructors-title />
                        <instructor-list instructor-number="T000875">
                           <instructor>Leonard Koff</instructor>
                           <instructor-credentials>PhD</instructor-credentials>
                           <instructor-biography>PhD, UC Berkeley; associate, UCLA Center for Medieval and Renaissance Studies.</instructor-biography>
                        </instructor-list>
                     </instructors>

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:text>&#xa;</xsl:text>
            <xsl:element name="ParaStyle--HeadList1">
                <xsl:variable name="title1"  select="name"/>
               
                <!--<xsl:value-of select="name"/>-->                                   
                <xsl:value-of select="$title1"/>
                               
            </xsl:element>
           
            <!--Carriage Return-->
            <xsl:text>&#xa;</xsl:text>
            <!--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>
           
            <xsl:text>&#xa;</xsl:text>           
            <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:element>
           
           
           
                                               
            <xsl:apply-templates select="program-area/program-stream/course"/>
        </xsl:element>           
    </xsl:template>
 


Thank you!
Radu
Posts: 5685

Re: XSL Getting every node instead of just one?

Mon Feb 06, 2017 5:18 pm

Hi,

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

https://www.oxygenxml.com/doc/versions/18.1/ug-editor/tasks/steps-in-typical-debug-process.html

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.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
cauldronofevil
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"?
Radu
Posts: 5685

Re: XSL Getting every node instead of just one?

Thu Feb 09, 2017 9:27 am

Hi,

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.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “Common Problems”

Who is online

Users browsing this forum: No registered users and 1 guest