LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2006, 05:58 AM   #1
Foomajick
LQ Newbie
 
Registered: Jan 2006
Location: Dublin
Distribution: Mandriva 2006 (desktop), Debian (server)
Posts: 16

Rep: Reputation: 0
XSLT transformation of XML document using XMLNS


Hello all, glad this forum is here because I'm sick of googling and LQ.org is my favorite forum for answers. Here's the short of it:

I need to transform XML documents from one schema to another using XSLT. The XML documents in question have their own XML namespaces in the root node.

Using oXygen's XSLT debugger, I can't get them to transform unless I comment out the XMLNS. This is both annoying and impractical, as I will be transforming thousands of documents and I would like to preserve the namespaces. oXygen uses Saxon 6.5.5 and Xalan transformation engines, and I am using XSL 1.0.

Does anyone have a solution? Also, the reason I'm asking now is because I'll be using Perl in the near future to do the job for me. If someone out there has experience with perl and XSLT and knows this NOT to be a problem, just as well. All the same, I'd love to have the answer because I'm a perfectionist! I just KNOW there is some <xsl:something> that will make it work, I just can't seem to find it. Here's some code for you:

XML:
Quote:
<?xml version="1.0" encoding="UTF-8"?>
<modsCollection xmlns="http://www.loc.gov/mods/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.loc.gov/mods/v3
http://www.loc.gov/standards/mods/v3/mods-3-2.xsd">
<mods>
<titleInfo>
<title>[Series of letters of congratulation to Michael Tierney on his appointment as President of University College Dublin.
1947.]</title>
</titleInfo>
<name type="personal">
<namePart>Tierney, Michael, 1894-1975</namePart>
<role>
<roleTerm>fmo</roleTerm>
</role>
</name>
<name type="personal">
<namePart>Duignan, Michael</namePart>
<role>
<roleTerm>aut</roleTerm>
</role>
</name>
... etc
</mods>
</modsCollection>
XSLT:
Quote:
<xsl:stylesheet version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlnsai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<xslutput method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/modsCollection">
<dc_collection>
<xsl:for-each select="mods">
<oai_dc:dc>
<dc:title>
<xsl:value-of select="titleInfo/title"/>
</dc:title>

...etc

</oai_dc:dc>
</xsl:for-each>
</dc_collection>
</xsl:template>

</xsl:stylesheet>
Thanks in advance this could really help me out alot! My boss thanks you too, by proxy. Enough info in this post to correctly identify the shadowy Foomajick...

Cheeers.
 
Old 09-27-2006, 11:26 AM   #2
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Rep: Reputation: 16
I don't know if you were after a different XSLT processor but I'd recommend using xsltproc (part of libxml - otherwise: http://www.xmlsoft.org/). It's a nice command-line utility and it works on your code without commenting out the namespaces (I checked).

Code:
xsltproc your.xsl your.xml > transformed_doc.xml
 
Old 10-18-2006, 05:31 AM   #3
Foomajick
LQ Newbie
 
Registered: Jan 2006
Location: Dublin
Distribution: Mandriva 2006 (desktop), Debian (server)
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks for thi info. This would all be alot easier if I were on a better (than XP) system. I can't seem to get onto Igor Zlatovic's site which is supposed to have Win binaries. I've Cygwin installed so if anyone has a nice solution for installing into that, I'm up for it. Otherwise, it's ok because (as you can probably tell from the month that has passed) this isn't a real bottleneck or anything. We'll survive!
 
Old 10-18-2006, 06:07 AM   #4
Foomajick
LQ Newbie
 
Registered: Jan 2006
Location: Dublin
Distribution: Mandriva 2006 (desktop), Debian (server)
Posts: 16

Original Poster
Rep: Reputation: 0
Transforming XML with XSLT from the command line in Windows XP

Quick update for anyone trying to do
Code:
xsltproc your.xsl your.xml > transformed_doc.xml
in Windows:

Download from ftp://ftp.zlatkovic.com/pub/libxml/ the following:

libxml2-2.6.26.win32.zip
libxslt-1.1.17.win32.zip
zlib-1.2.3.win32.zip
iconv-1.9.2.win32.zip

You may not need the last two, but I got them just in case.

Unzip them, put them anywhere you want. Then copy the contents of each /bin (inside each of the four packages) to a directory you have listed in your PATH environment variable. For me, that's my own homemade /bin directory, which I created so that I can pretend like I'm on a Linux system at work.

Then you can work away with your xslt transformations in the command line.

I found this about a thousand times quicker from the command line when processing large xml files.

Unfortunately, my first problem is not resolved! I got the same results, if I have an XMLNS in the code, it doesn't work. I'm beginning to suspect it might be the location of the XMLNS not being good. Will keep experimenting.
 
Old 10-18-2006, 01:31 PM   #5
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Rep: Reputation: 16
That is strange as the code you posted worked fine with my installation of xsltproc. I see you have abridged it though.

Last edited by senyahnoj; 10-18-2006 at 01:32 PM.
 
Old 10-19-2006, 08:05 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
oXygen uses Saxon 6.5.5
The latest version of Saxon is version 8.8.

I would try addressing the above first.

You can always go to the Saxon website and post a question,
make it clear with examples.

I've done this before and the Saxon maintainer himself
mailed me back a solution after a day or so.

I use a bit of XML and use saxon as standalone.
The advantage with saxon is it also includes an implementation
of Xquery.

I would prefer to use perl but haven't been able to find anything that does all saxon does. You get XSLT processors but I never found an xquery module.
Mind you I only had a dig on CPAN for an afternoon.

Edit:
oops, read further it says the XSLT part is version 6.5.5 ???
but saxon is 8.8, confused me. ???

Last edited by bigearsbilly; 10-19-2006 at 08:14 AM.
 
Old 10-20-2006, 09:11 AM   #7
Foomajick
LQ Newbie
 
Registered: Jan 2006
Location: Dublin
Distribution: Mandriva 2006 (desktop), Debian (server)
Posts: 16

Original Poster
Rep: Reputation: 0
Well I've done some more experimenting, and to make it plainer, here's better code. The XML is validated with the XMLNS included. The stylesheet is complete, and XSLT 2.0. It's fairly straightforward.

XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<mods xmlns="http://www.loc.gov/mods/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink"
    xsi:schemaLocation="http://www.loc.gov/mods/v3         http://www.loc.gov/standards/mods/v3/mods-3-2.xsd" version="3.2">
    <titleInfo>
        <title>[Series of letters of congratulation to Michael Tierney on his appointment as President of University College Dublin. 1947.]</title>
    </titleInfo>
    <name type="personal">
        <namePart>Tierney, Michael, 1894-1975</namePart>
        <role>
            <roleTerm>fmo</roleTerm>
        </role>
    </name>
    <name type="personal">
        <namePart>Duignan, Michael</namePart>
        <role>
            <roleTerm>aut</roleTerm>
        </role>
    </name>
</mods>
XSL
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <!-- Converts MODS files to html.  -->
    <xsl:template match="/mods">
        <html>
            <head>
                <title>MODS Record</title>
            </head>
            <body>
                <table border="1">
                    <xsl:for-each select="child::*">
                        <tr>
                            <td>
                                <xsl:value-of select="name()"/>
                            </td>
                            <td>
                                <xsl:value-of select="current()"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
Now. It only outputs the values of the XML tags if I leave in the XMLNS in the <mods> element. That goes for oXygen AND xsltproc. (Using xsltproc, I've had to revert to XSLT 1.0 as well (just changed that one little number in the xslt from 2.0 to 1.0)). Perhaps the Windows version of xsltproc is dated?)

If I remove the xmlns info, it works in oXygen and through xsltproc, and produces a nice little html table.

Given that

1. The xmlns and subsequent xml are valid;
2. it breaks using the command line utility, and using oXygen (using Saxon 6.5.5 (xslt 1.0), Saxon8B (xslt2.0), and Xalan parsers)
and
3. it works without the xmlns in all of the above mentioned methods,

does this not sound like an XSLT problem, rather than a parser problem?

Thanks for the input so far, it's getting me going on it. I might try and post this to the StylusStudio forum where I've seen alot of XML discussion, even though I don't own a license!

Foo Foo
 
Old 10-25-2006, 05:27 AM   #8
Foomajick
LQ Newbie
 
Registered: Jan 2006
Location: Dublin
Distribution: Mandriva 2006 (desktop), Debian (server)
Posts: 16

Original Poster
Rep: Reputation: 0
Solution: XSLT transformation of XML document using XMLNS

Hi, I posted to the MODS listserve and the folks there had the answer. This is not specific to the MODS schema, it goes for any XML XSLT transformation involving namespaces, so hopefully others may find this helpful.

For your info:

Quote:
If the source XML contains namespace information (xmlns), associate the
namespace ('http://www.loc.gov/mods/v3') with the prefix 'mods' in the
XSLT template. Then prefix all mods elements called by the XSLT with
"mods:", for example:
<xsl:value-of match="mods:mods/mods:subject/mods:topic"/>

A perfect example is given in the first response below.

Regards,


>Take a look at the following from the MODS site:
>http://www.loc.gov/standards/mods/mods.xsl

>This stylesheet converts MODS to a 'simple HTML display'. The opening
>xsl:stylesheet element:

><xsl:stylesheet version="1.0"
>xmlns:xlink="http://www.w3.org/1999/xlink"
>xmlns:mods="http://www.loc.gov/mods/v3"
>xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>exclude-result-prefixes="mods" >

>associates the namespace ('http://www.loc.gov/mods/v3') with the
>prefix 'mods' you can use to reference elements in the namespace.
>I've always found it useful to start from a known working state, so I
>hope this gets you started.

>Good luck --

>Joe


>>Are you declaring the mods namespace and binding it to a prefix in the
>>stylesheet? E.g. you should have templates with xpaths like
>>"m:titleInfo" where the "m" prefix binds to the correct URI.

>>Bruce



>>>Here's a guess, though: you have xsl templates matching mods
elements, but the templates aren't name-spaced; e.g., you have

>>><xsl:template match="titleInfo"> ...</xsl:template>

>>>which will work fine if you try to match against an un-namespaced
mods record but not a (proper) one in the mods namespace. For that you need

>>><xsl:stylesheet xmlns:mods="http://www.loc.gov/mods/v3">

>>><xsl:template match="mods:titleInfo">...</xsl:template>

>>></xsl:stylesheet>
Hope helps anyone with a similar problem.

Foo
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Update Your XML with XSLT, Apache Ant, and Java SE LXer Syndicated Linux News 0 08-24-2006 07:54 PM
suggestions for using xslt to view base64-encoded floating point data in an xml file? zero79 Programming 0 01-10-2006 06:52 PM
XSLT/xml enforcing full tag syntax bigearsbilly Programming 2 11-23-2005 02:38 AM
Reading a xml document: amirspl Programming 3 09-10-2003 07:51 AM
ePlease help-> XML, XSLT James Clarkes XT software Al Bundy Linux - Software 0 04-21-2003 06:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration