Page 1 of 1

XSL Getting every node instead of just one?

Posted: Fri Feb 03, 2017 1:33 am
by cauldronofevil
Okay, I'm a newbie as well, so please forgive.

Here is my XML file:
<name>Writing, Journalism & Literature</name>
<category-description/><program-area code="PA0073">
<name>Comparative & 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 & Literature</ParaStyle--HeadList1>
<ParaStyle--HeadList2>Comparative & 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 & Literature</ParaStyle--HeadList1>
<ParaStyle--HeadList2>Comparative & English Literature</ParaStyle--HeadList2>

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

Thank you!

Re: XSL Getting every node instead of just one?

Posted: Fri Feb 03, 2017 11:52 am
by adrian
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

Re: XSL Getting every node instead of just one?

Posted: Fri Feb 03, 2017 9:12 pm
by cauldronofevil
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 & Literature</name>
<category-description />
<program-area code="PA0073">
<name>Comparative & English Literature</name>
<program-area-description />
<program-stream code="PS0199">
<program-stream-name>Comparative Literature</program-stream-name>
<program-stream-description>&lt;p&gt;&lt;em&gt;For more information call (310) 825-7093.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;See also Literature, page 159.&lt;/strong&gt;&lt;/p&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 &quot;enlightened&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&#39;s Essay on Man, Voltaire&#39;s &lt;em&gt;Candide&lt;/em&gt;, and Goethe&#39;s &lt;em&gt;Faust&lt;/em&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.&nbsp;</description>
<catalog-description />
<outcomes>
<outcomes-title>Topics include:</outcomes-title>
<outcome />
</outcomes>
<notes>
<notes-title>Note(s):</notes-title>
<note>&lt;b&gt;&lt;u&gt;Contact:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Jennifer Polutan&lt;br /&gt;
(310) 825-7093&lt;br /&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>&lt;p&gt;Applies toward the Literary and Cultural Analysis requirement under the Foundation of Arts and Humanities.&lt;br /&gt;
Midterm and final exams are proctored online; additional requirements include microphone, headphones/speakers, and webcam.&lt;br /&gt;
Enrollment limited to 25 students.&lt;br /&gt;
Enrollment deadline: April 12, 2017.&nbsp;&lt;br /&gt;
No refund after April 12, 2017. Materials required. Internet access required.&nbsp;&lt;/p&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,'&','0x0026')"/>-->
<!--<xsl:variable name="title3" select="replace($title3,'&','0x0026')"/>-->
<xsl:value-of select="$title4"/>
</xsl:element>




<xsl:apply-templates select="program-area/program-stream/course"/>
</xsl:element>
</xsl:template>
Thank you!

Re: XSL Getting every node instead of just one?

Posted: Mon Feb 06, 2017 5:18 pm
by Radu
Hi,

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

https://www.oxygenxml.com/doc/versions/ ... ocess.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

Re: XSL Getting every node instead of just one?

Posted: Wed Feb 08, 2017 9:12 pm
by cauldronofevil
What "various variables"? I've only got the one "title3"?

Re: XSL Getting every node instead of just one?

Posted: Thu Feb 09, 2017 9:27 am
by Radu
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