Reputation: 9
I have very limited experience using XML and XSLT. I’m working with a large amount (about 4,000) highly standardized XML files. I’m very hesitant to edit the XML files, instead I would just like to code an XSLT file to display the tags nicely. The problem I have is that the root element has several namespaces declared on it.
When there is only one namespace, everything works fine.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ex10.xsl"?>
<record xmlns="http://www.loc.gov/MARC21/slim">
<LCCN>05040166</LCCN>
<call>GN800.B62</call>
<author>Edward Thomas Stevens</author>
<title>Flint chips</title>
<publisher>London: Bell and Daldy, 1870.</publisher>
<subject>Archaeology--Stone age.</subject>
</record>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://www.loc.gov/MARC21/slim" exclude-result-prefixes="a" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<p>
<xsl:value-of select="a:record/a:title"/>
by <xsl:value-of select="a:record/a:author"/>
, located <xsl:value-of select="a:record/a:call"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
But when there is one more namespace declared on the root element, I don’t know what to do.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ex10.xsl"?>
<record xmlns="http://www.loc.gov/MARC21/slim" xmlns:cinclude="http://apache.org/cocoon/include/1.0">
<LCCN>05040166</LCCN>
<call>GN800.B62</call>
<author>Edward Thomas Stevens</author>
<title>Flint chips</title>
<publisher>London: Bell and Daldy, 1870.</publisher>
<subject>Archaeology--Stone age.</subject>
</record>
The result I am trying to get is an plain text statement of TITLE by AUTHOR, located at CALL NUMBER. But when I attach the second XML file to the previous XSLT file, it no longer displays properly. I'm assuming this is because of the second namespace declaration, as that is the only thing changed. I would like to get the original result even with the second namespace defalcation.
I don't want to delete the extra namespaces because they are part of a standard (MARCXML standard by the Library of Congress). I would like to keep the original XML files as un-edited as possible.
Upvotes: 0
Views: 107
Reputation: 1235
Whenever you have a namespace in your XML that does not have a prefix, you have to put it into the XSLT stylesheet tag with a prefix. And you can NOT add the prefix you used to exclude-result-prefixes. Instead, you have to use the prefix in your code to reference elements in that namespace. In the code below, I changed the "/" to "record" in your template match, so I could use the namespace.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.loc.gov/MARC21/slim"
xmlns:cinclude="http://apache.org/cocoon/include/1.0"
exclude-result-prefixes="cinclude" version="1.0">
<xsl:output method="html"/>
<xsl:template match="a:record">
<html>
<body>
<p>
<xsl:value-of select="a:title"/>
by <xsl:value-of select="a:author"/>
, located <xsl:value-of select="a:call"/>
</p>
</body>
</html>
</xsl:template>
Upvotes: 1