0

I apologize for what must be the hundredth question about xsl:template and namespaces. I did read other questions (for example here, here and here), but still cannot put the pieces together.

This is my input:

<?xml version="1.0" encoding="UTF-8" ?>
<AuthorIT version="6.2.0" xmlns="http://www.authorit.com/xml/authorit"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
    <Objects>
        <Book>
            <Object>
                <Description>1089-802-01</Description>
            </Object>
        </Book>
    </Objects>
</AuthorIT>

This is my stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ait="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
<xsl:template match="/">
    <Table>
        <apply-templates/>
    </Table>
</xsl:template>
<xsl:template match="ait:Book">
    <Row>
        <Cell>
            <Data><xsl:value-of select="./Object/Description"/></Data>
        </Cell>
    </Row>
</xsl:template>

Expected result:

<Table>
    <Row>
        <Cell>
            <Data>1089-802-01</Data>
        </Cell>
    </Row>
</Table>

Actual result:

<Table>
       1089-802-01
</Table>

In other words, xsl:template match="/" matches, but xsl:template match="ait:Book" does not.

How do I match the Book element?

Note that I took the default namespace from the input root element AuthorIT xmlns="http://www.authorit.com/xml/authorit" and added it to the stylesheet as xmlns:ait="http://www.authorit.com/xml/authorit". I then try to match Book elements as ait:Book. But I must have misunderstood this answer.

I do not know if it is important, but I am using Saxon HE 9 and the Notepad++ XML plugin.

Community
  • 1
  • 1

4 Answers4

0

Well <apply-templates/> is a literal result element, you would need <xsl:apply-templates/>. But I don't get the result (http://xsltransform.net/ej9EGcB) you say you get, I get

<?xml version="1.0" encoding="UTF-8"?><Table xmlns:ait="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><apply-templates/></Table>

which makes it clear that you simply create a Table element with a apply-templates child element.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

As Martin Honnen already pointed out, the actual result received using your code is not what you claim.

Anyway, to get the expected result, you must change

<xsl:value-of select="./Object/Description"/> 

to:

<xsl:value-of select="./ait:Object/ait:Description"/>

because these elements are in the given namespace too.

You should also add exclude-result-prefixes="ait" as an attribute of the xsl:stylesheet element, and remove the redundant namespace declarations there.

This is in addition to the <apply-templates/> vs. <xsl:apply-templates/> issue.

And of course you need to a closing </xsl:stylesheet> tag.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

Thank you all for the answers.

I did the following:

  • Added the xmlns:ait="http://www.authorit.com/xml/authorit" declaration to the stylesheet tag.
  • Added the ait: prefix where relevant.
  • Added exclude-result-prefixes="ait" to the stylesheet tag.

It works now, although I do not understand what exclude-result-prefixes="ait" does: the transformation goes well with and without it. Maybe a topic for future research.

The <apply-templates/> vs. <xsl:apply-templates/> issue and the closing </xsl:stylesheet> tag were typos that slipped in when simplifying the files for the post. Sorry about that and thanks for pointing it out.

Particular thanks to Martin Honnen for the link to http://xsltransform.net: I did not know it existed and it is very handy.

-1

try using

<xsl:apply-templates/>

(you are missing the "xsl:") and

<xsl:template match="AuthorIT/Object/Books">

instead of the "ait:Book". This should do the trick.

Martin Vitek
  • 159
  • 4