Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 17th, 2018, 10:14 AM
Registered User
Points: 22, Level: 1
Points: 22, Level: 1 Points: 22, Level: 1 Points: 22, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2018
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trying to transform XML documents will XSLT

I am way over my head in a very basic XML class and I was hoping that someone would be so kind as to offer some thoughts. I have three XML records and an XSD schema. I need to transform all three XML documents through an XSLT file to:


Create XSLT to display all of my records. I should only display the following fields: creatorsí name, forename, title, format, publication date.

Then I need to order the display by publication date, oldest first and display all the creators alphabetized by last name. Your XSLT must do this even if none of your items have multiple creators.

I have been able to create my XML files but honestly, I am just stuck in re my XSLT file and I hope someone mind be willing to reduce the number of tears I have to shed. Thanks.


The XSD schema:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="catalog">
<xs:annotation>
<xs:documentation>Schema for Assignment 3 of Info 657 that mimics some of the bibliographic elements of MODS in a simpler form</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="catalogRecord" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="titleInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="subtitle" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!--titleInfo -->
<xs:element name="nameInfo" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<!-- use the "name" field for the surname or only-name like Prince -->
<!-- only mark ONE creator as primary; there is a way to enforce this but it's too complex for this assignment -->
<xs:element name="name" type="xs:string"/>
<xs:element name="forename" type="xs:string" minOccurs="0"/>
<xs:element name="role">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="author"/>
<xs:enumeration value="editor"/>
<xs:enumeration value="performer"/>
<xs:enumeration value="director"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="primary" type="xs:boolean" use="required"/>
</xs:complexType>
</xs:element>
<!--nameInfo -->
<xs:element name="originInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="place" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>
<xs:element name="dateIssued" type="xs:gYear"/>
<xs:element name="edition" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!--originInfo -->
<xs:element name="typeOfResource">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="book"/>
<xs:enumeration value="sound recording"/>
<xs:enumeration value="film"/>
<xs:enumeration value="score"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<!--typeOfResource -->
<xs:element name="subject" type="xs:string" minOccurs="2" maxOccurs="unbounded">
<!--you may use free-text tags or LCSH subjects -->
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


My XML files:

<?xml version="1.0" encoding="UTF-8"?>

<catalog xsi:noNamespaceSchemaLocation="catalogschema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<catalogRecord>
<titleInfo>
<title>The Selfish Gene</title>
<subtitle></subtitle>
</titleInfo>
<nameInfo primary="false">
<name>Dawkins</name>
<forename>Richard</forename>
<role>author</role>
</nameInfo>
<originInfo>
<place>Oxford, England</place>
<publisher>Oxford University Press</publisher>
<dateIssued>2006</dateIssued>
<edition>3</edition>
</originInfo>
<typeOfResource>book</typeOfResource>
<subject>Biology</subject>
<subject>Evolution</subject>
<subject>Genetics</subject>
<subject>Acedemic Publishing</subject>
</catalogRecord>
</catalog>


<catalog xsi:noNamespaceSchemaLocation="catalogschema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<catalogRecord>
<titleInfo>
<title>The Heart of Saturday Night</title>
<subtitle></subtitle>
</titleInfo>
<nameInfo primary="false">
<name>Waits</name>
<forename>Tom</forename>
<role>performer</role>
</nameInfo>
<nameInfo primary="false">
<name>Howe</name>
<forename>Bones</forename>
<role>director</role>
</nameInfo>
<originInfo>
<place>Los Angelos, CA</place>
<publisher>Asylum Records</publisher>
<dateIssued>1974</dateIssued>
<edition>1</edition>
</originInfo>
<typeOfResource>sound recording</typeOfResource>
<subject>Jack Kerouac</subject>
<subject>Southern California</subject>
<subject>Love</subject>
</catalogRecord>
</catalog>

<?xml version="1.0" encoding="UTF-8"?>

<?xml version="1.0" encoding="utf-8"?>

<catalog xsi:noNamespaceSchemaLocation="catalogschema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<catalogRecord>
<titleInfo>
<title>Fences</title>
<subtitle></subtitle>
</titleInfo>
<nameInfo primary="false">
<name>August</name>
<forename>Wilson</forename>
<role>Author</role>
</nameInfo>
<nameInfo primary="false">
<name>Denzel Washington</name>
<role>Director</role>
</nameInfo>
<nameInfo primary="false">
<name>Viola Davis</name>
<role>Performer</role>
</nameInfo>
<originInfo>
<place>Hollywood, CA</place>
<publisher>Paramount Pictures</publisher>
<dateIssued>2016</dateIssued>
<edition></edition>
</originInfo>
<typeOfResource>film</typeOfResource>
<subject>African American families</subject>
<subject>Family</subject>
<subject>Pittsburg, PA</subject>
<subject>1950's</subject>
<subject>American history</subject>
</catalogRecord>
</catalog>

Thank you so much.
Reply With Quote
  #2 (permalink)  
Old May 17th, 2018, 10:35 AM
mhkay's Avatar
Wrox Author
Points: 18,392, Level: 59
Points: 18,392, Level: 59 Points: 18,392, Level: 59 Points: 18,392, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,947
Thanks: 0
Thanked 288 Times in 283 Posts
Default

I'm happy to help you debug your code if you're completely stuck, but I'm not going to write the code for you. Show how far you've got, and indicate how it's failing.

Note that to post code on this site and retain the formatting, use [ code ] and [/ code ] start and end tags to delimit it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old May 17th, 2018, 10:37 AM
Registered User
Points: 22, Level: 1
Points: 22, Level: 1 Points: 22, Level: 1 Points: 22, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2018
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was really hoping for suggestions on where to start. I really donít know. If I ever finish it Iíll post it. Thanks
Reply With Quote
  #4 (permalink)  
Old May 17th, 2018, 11:52 AM
mhkay's Avatar
Wrox Author
Points: 18,392, Level: 59
Points: 18,392, Level: 59 Points: 18,392, Level: 59 Points: 18,392, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,947
Thanks: 0
Thanked 288 Times in 283 Posts
Default

Start by reading my book.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #5 (permalink)  
Old May 19th, 2018, 05:19 PM
Registered User
Points: 22, Level: 1
Points: 22, Level: 1 Points: 22, Level: 1 Points: 22, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2018
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multiple XML documents transformed into one table?

I have been reading and trying but I just feel really stupid. This is what I have so far and I will keep trying to figure it out. One question though: Where or how does the XSLT file transform elements from multiple XML files into a single HTML table (I hope that I am using the terms correctly)? I think that is what I am trying to figure out. Thanks.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<body>
<table>
<tr>
<th>Creator</th>
<th>Title</th>
<th>Format</th>
<th>Date</th>
</tr>
<xsl:apply-templates />
<tr>
<xsl:for-each select="catalog/catalogRecord">
<td>
<xsl:value-of select="catalog/catalogRecord/nameInfo/name" />
</td>
</xsl:for-each>
</tr>
</table>

</body>
</html>
</xsl:template>
</xsl:stylesheet>
Reply With Quote
  #6 (permalink)  
Old May 19th, 2018, 05:26 PM
mhkay's Avatar
Wrox Author
Points: 18,392, Level: 59
Points: 18,392, Level: 59 Points: 18,392, Level: 59 Points: 18,392, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,947
Thanks: 0
Thanked 288 Times in 283 Posts
Default

To read multiple XML files in a transformation you will generally want to use the document() function.

For example if your main input document contains a list of files to be processed like this:

Code:
<docs>
  <doc href="doc1.xml"/>
  <doc href="doc2.xml"/>
  <doc href="doc3.xml"/>
  <doc href="doc4.xml"/>
</docs>
then you might use <xsl:for-each select="document(/docs/doc/@href)"/> to process them.

Of course the detail depend on how your stylesheet finds out what input documents it needs to process, and on whether it is applying the same processing to all input documents, or different processing.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #7 (permalink)  
Old May 19th, 2018, 05:33 PM
Registered User
Points: 22, Level: 1
Points: 22, Level: 1 Points: 22, Level: 1 Points: 22, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2018
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That help so much Thanks.
Reply With Quote
  #8 (permalink)  
Old May 20th, 2018, 11:38 AM
Registered User
Points: 22, Level: 1
Points: 22, Level: 1 Points: 22, Level: 1 Points: 22, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2018
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Top level elements must have a non null namespace error

I keep getting this namespace error and I don't really understand why. Isn't the "xmlns:..." a suitable napespace? Do I have top level elements that I don't need?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<catalog>
<catalogRecord>

<docs>
<doc href="TheHeartofSaturdayNight.xml"/>
<doc href="Fences.xml"/>
<doc href="TheSelfishGene.xml"/>

</docs>

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="document(/docs/doc/@href)"/>
<table>
<tr>
<th>Creator</th>
<th>Title</th>
<th>Format</th>
<th>Date</th>
</tr>
<xsl:apply-templates />
<tr>
<xsl:for-each select="catalog/catalogRecord">
<td>
<xsl:value-of select="catalog/catalogRecord/nameInfo/name" />
</td>
</xsl:for-each>
</tr>
</table>

</body>
</html>
</xsl:template>
</catalogRecord>
</catalog>
</xsl:stylesheet>
Reply With Quote
  #9 (permalink)  
Old May 20th, 2018, 11:59 AM
Friend of Wrox
Points: 6,612, Level: 34
Points: 6,612, Level: 34 Points: 6,612, Level: 34 Points: 6,612, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,234
Thanks: 0
Thanked 242 Times in 241 Posts
Default

You need to have that list of files in a separate XML document (e.g. "file-list.xml") and then you can read in for-each select="document(document("file-list.xml")//docs/doc/@href)//catalog/catalogRecord". Inside of your XSLT code you need to have xsl:template as children of the xsl:stylesheet element, not inside of any other elements.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #10 (permalink)  
Old May 20th, 2018, 12:07 PM
Registered User
Points: 22, Level: 1
Points: 22, Level: 1 Points: 22, Level: 1 Points: 22, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2018
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks

Okay, that makes sense. I appreciate it.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
transform xml to html with through xslt vishu16.vishwas@gmail.com XSLT 6 January 11th, 2016 03:33 AM
xml flat file transform to nested tag xml using xslt transform adsingh XSLT 0 August 7th, 2013 01:27 PM
how to actually transform an XML with XSLT? ovlique BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 2 June 27th, 2010 03:06 PM
Using XSLT to transform one XML format to other sivakumar_g01 XSLT 3 June 14th, 2010 09:45 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM



All times are GMT -4. The time now is 06:23 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.