Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT applied to XML doc with xmlns attribute

I'm applying an XSLT stylesheet to the following XML file:

<top xmlns="http://www.foo.com/bar">
    <elementA />
    <elementB />
    <contents>
        <contentitem>
                <id>3</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
        <contentitem>
                <id>2</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
        <contentitem>
                <id>1</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
    </contents>
</top>

Here's my current XSLT file (performs a simple sort):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:doc="http://www.foo.com/bar">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
<!--                                                -->
 <xsl:strip-space elements="*"/>
<!--                                                -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
<!--                                                -->
  <xsl:template match="contents">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="contentitem">
        <xsl:sort select="id" data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Problem is, I do not know exactly how to use the 'doc:' namespace prefix with the xsl:template and xsl:apply-templates tags.

Right now, the XML document is copied as-is, so I believe the first xsl:template block is being applied. However, the items are unsorted, so I think the problem lies in the second xsl:template.

I should note that if I remove the xmlns attributes from both files, the transformation works properly.

Any suggestions?

(question is based on this example)

like image 539
Jeremy Avatar asked Sep 09 '09 18:09

Jeremy


People also ask

How do I link XML to XSLT?

Execute an XSLT transformation from an XML fileOpen an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document.

What is the xmlns attribute?

The xmlns attribute specifies the xml namespace for a document. Note: The xmlns attribute is required in XHTML, invalid in HTML 4.01, and optional in HTML5. Note: The HTML validator at http://w3.org does not complain when the xmlns attribute is missing in an XHTML document.

Is XSLT a XML document?

Extensible Stylesheet Language Transformations (XSLT) is an XML-based language used, in conjunction with specialized processing software, for the transformation of XML documents.

Can we use XQuery in XSLT?

You can call any of the MarkLogic Server Built-In XQuery functions from an XSLT stylesheet.


1 Answers

Have you tried prefixing element names with the doc: namespace prefix in your select attributes?

<xsl:template match="doc:contents">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="doc:contentitem">
      <xsl:sort select="doc:id" data-type="number"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
like image 158
Olivier 'Ölbaum' Scherler Avatar answered Oct 13 '22 13:10

Olivier 'Ölbaum' Scherler