Francesco
Francesco

Reputation: 2382

Transform an xml to xml using XSLT

I'm very new to XSLT (for ~1 hour) and unfortunately at the moment I don't have too much time to learn it (I have a milestone) so I would like to know if there is someone who can help me to define a XSLT to transform such a file:

<?xml version="1.0" encoding="UTF-8"?>
<epics-doi xmlns:dc="http://dublincore.org/documents/dcmi-namespace/">
  <record>
    26.03.2012PK_000404500
    <dc:identifier>a</dc:identifier>
    313
    <dc:identifier>1</dc:identifier>
  </record>
  <record>
    26.03.2012PK_000404500
    <dc:identifier>b</dc:identifier>
    313
    <dc:identifier>2</dc:identifier>
  </record>
  ...
</epics-doi>

Into this:

<?xml version="1.0" encoding="UTF-8" ?>
<OAI-PMH>
    <responseDate></responseDate>
    <request></request>
    <ListRecords>
        <record>
            <header>
                <identifier></identifier>
                <datestamp></datestamp>
                <setSpec></setSpec>
            </header>
            <metadata>
                <oai_dc:dc>
                    <dc:type></dc:type>
                    <dc:language></dc:language>
                    <dc:identifier>a</dc:identifier>
                    <dc:title></dc:title>
                    <dc:publisher></dc:publisher>
                    <dc:publisher></dc:publisher>
                    <dc:date></dc:date>
                    <dc:format></dc:format>
                    <dc:creator></dc:creator>
                    <dc:contributor></dc:contributor>
                    <dc:identifier>1</dc:identifier>
                </oai_dc:dc>
            </metadata>
        </record>
        <record>
            <header>
                <identifier></identifier>
                <datestamp></datestamp>
                <setSpec></setSpec>
            </header>
            <metadata>
                <oai_dc:dc>
                    <dc:type></dc:type>
                    <dc:language></dc:language>
                    <dc:identifier>b</dc:identifier>
                    <dc:title></dc:title>
                    <dc:publisher></dc:publisher>
                    <dc:publisher></dc:publisher>
                    <dc:date></dc:date>
                    <dc:format></dc:format>
                    <dc:creator></dc:creator>
                    <dc:contributor></dc:contributor>
                    <dc:identifier>2</dc:identifier>
                </oai_dc:dc>
            </metadata>
        </record>
        ...
    </ListRecords>
</OAI-PMH>

I tried this(leaving away some tags):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
   <xsl:template match="/">
      <xsl:apply-templates select="/epics-doi/record/metadata" />
   </xsl:template>
   <xsl:template match="metadata">
      <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/dc/terms/" 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">
         <xsl:for-each select="dc:identifier">
            <dc:identifier>
               <xsl:value-of select="dc:identifier" />
            </dc:identifier>
         </xsl:for-each>
      </oai_dc:dc>
   </xsl:template>
</xsl:stylesheet>

but I don't know if I'm on the right track, as if I load the file in Firefox, the page remains blank...

Thank you very much in advance!


Thank you Kevan. Your XSLT is right, but it has one problem: if I have more than one record (as in the above example), I get two records in the resulting XML too, but in each record there are 4 dc:identifier; each identifier is added in each record... :

<?xml version="1.0" encoding="utf-8"?>
<OAI-PMH xmlns:dc="http://dublincore.org/documents/dcmi-namespace/"
    xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/">
    <responseDate/>
    <request/>
    <ListRecords>
        <record>
            <header>
                <identifier/>
                <datestamp/>
                <setSpec/>
            </header>
            <metadata>
                <oai_dc:dc>
                    <dc:type/>
                    <dc:language/>
                    <dc:title/>
                    <dc:publisher/>
                    <dc:publisher/>
                    <dc:date/>
                    <dc:format/>
                    <dc:creator/>
                    <dc:contributor/>
                    <dc:identifier>a</dc:identifier>
                    <dc:identifier>1</dc:identifier>
                    <dc:identifier>b</dc:identifier>
                    <dc:identifier>2</dc:identifier>
                </oai_dc:dc>
            </metadata>
        </record>
        <record>
            <header>
                <identifier/>
                <datestamp/>
                <setSpec/>
            </header>
            <metadata>
                <oai_dc:dc>
                    <dc:type/>
                    <dc:language/>
                    <dc:title/>
                    <dc:publisher/>
                    <dc:publisher/>
                    <dc:date/>
                    <dc:format/>
                    <dc:creator/>
                    <dc:contributor/>
                    <dc:identifier>a</dc:identifier>
                    <dc:identifier>1</dc:identifier>
                    <dc:identifier>b</dc:identifier>
                    <dc:identifier>2</dc:identifier>
                </oai_dc:dc>
            </metadata>
        </record>
    </ListRecords>
</OAI-PMH>

Upvotes: 1

Views: 2371

Answers (3)

Francesco
Francesco

Reputation: 2382

I modified your answer this way and now it works...

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dc="http://dublincore.org/documents/dcmi-namespace/"
    xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/">
    <xsl:output method="xml"/>

    <xsl:template match="/">
        <OAI-PMH>
            <ListRecords>
                <xsl:apply-templates select="epics-doi/record"/>
            </ListRecords>
        </OAI-PMH>
    </xsl:template>

    <xsl:template match="record">
        <record>
            <header>
            </header>
            <metadata>
                <oai_dc:dc>
                    <xsl:apply-templates select="dc:identifier"/>
                </oai_dc:dc>
            </metadata>
        </record>
    </xsl:template>

    <xsl:template match="dc:identifier">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Thank you again.

Upvotes: 0

Kevan
Kevan

Reputation: 891

Try this:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dc="http://dublincore.org/documents/dcmi-namespace/"
    xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/">
    <xsl:output method="xml"/>

   <xsl:template match="/">
       <OAI-PMH>
           <responseDate></responseDate>
           <request></request>
           <ListRecords>
               <xsl:apply-templates select="/epics-doi/record"/>
           </ListRecords>
       </OAI-PMH>
   </xsl:template>

    <xsl:template match="/epics-doi/record">
        <record>
            <header>
                <identifier></identifier>
                <datestamp></datestamp>
                <setSpec></setSpec>
            </header>
            <metadata>
                <oai_dc:dc>
                    <dc:type></dc:type>
                    <dc:language></dc:language>
                    <dc:title></dc:title>
                    <dc:publisher></dc:publisher>
                    <dc:publisher></dc:publisher>
                    <dc:date></dc:date>
                    <dc:format></dc:format>
                    <dc:creator></dc:creator>
                    <dc:contributor></dc:contributor>
                    <xsl:apply-templates select="/epics-doi/record/dc:identifier"/>
                </oai_dc:dc>
            </metadata>
        </record>
    </xsl:template>

    <xsl:template match="/epics-doi/record/dc:identifier">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

One problem I noticed is that the dc name space in the XML is "http://dublincore.org/documents/dcmi-namespace/ but it's "http://purl.org/dc/elements/1.1/" in the XSL - I don't think that can work

Upvotes: 3

gmalette
gmalette

Reputation: 2459

<xsl:apply-templates select="/epics-doi/record/metadata"/>

Your /epics-doi/record node doesn't contain any metadata node, so this will not match anything.

You should use

<xsl:apply-templates select="/epics-doi/record"/>

along with

<xsl:template match="record">
   <!-- Transformation here -->
</xsl:template>

Tip: You can use a tool like oXygen to develop XSLT (sorry, I never used any so I don't know open-source ones)

UPDATE:

Also, you shouldn't need to use dc: in the foreach select, plus inside the loop you've already selected the node. I think it should look more like:

<xsl:for-each select="identifier">
    <dc:identifier>
        <xsl:value-of select="."/>
    </dc:identifier>
</xsl:for-each>

Upvotes: 2

Related Questions