0

I've looked at a couple of different stackoverflow answers regarding this issue, but as I am new to this transformation topic, I feel like I have made a obvious mistake I simply cannot see.

Here is my xml

<application xmlns:os="http://url..." name="test">

<catalog>
  <cd>
    <title> Empire Burlesque </title>
    <artist> Bob Dylan </artist>
    <country> USA </country>
    <company> Columnbia </company>
  </cd>
</catalog>

</application>

Here is my xsl

<?xml version="1.0" encoding="UFT-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:os="http://url...">
<xsl:output method="html" version="1.0" encoding=ISO-8859-1" indent="yes"/>


<xsl:template match="application">

<html>
<body>
  <h2>
    <th style="text-align:left">Title</th>
    <th style="text-align:left">Artist</th>
  </h2>

  <!-- this doesn't work -->
  <xsl:for-each select="os:catalog/cd">
    <h2><xsl:value-of select="title"></h2>
    <h2><xsl:value-of select="artist"></h2>
  </xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>

The output is currently:

Title Artist

But I want the output to be:

Title Artist

Empire Burlesque Bob Dylan


I'm not sure what the error is. I tried setting the template as / but that didn't seem to work either

1 Answer 1

1

Besides missing a closing quote, "os:catalog/cd won't match because the catalog element is not in a namespace in your XML. Change xsl:for-each/@select to "catalog/cd" to match the catalog/cd elements in your XML.

Understand that the declaration of a namespace prefix,

xmlns:os="http://url..." 

does not imply that the namespace is actually used anywhere in the XML; in the case of your XML, it indeed is not.

Contrast this with a default namespace declaration,

xmlns="http://url..." 

which automatically places the associated element and its descendents in the default namespace.

Sign up to request clarification or add additional context in comments.

4 Comments

So if I were to remove the prefix, the catalog/cd would match since it "automatically places the associated element and its desendents in the default namespace"?
Removing the os prefix would allow catalog/cd to match because the os namespace prefix is declared but never used in your XML. Your XML does not have a default namespace; it has a declaration for an unused namespace prefix.
So in the working version (I can't test it at the moment), it would be what I have, but every instance of the prefix os in both the xml and xsl file would be gone. Is this correct? If so, can I change the template to match / (everything), or it must be application?
You seem to require more attention on this topic than is appropriate for the basic Q/A format of this site. Please accept this answer if it's helped, work further on your problem, and ask a new question if necessary. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.