Hi,

I am trying to parse an xml which i am getting in a variable in my xsl. till the time i do not have any namespace in my xml i can parse all the tags of my xml from my xsl, however, for any tag with a namespace i could not parse it. not sure why is it so.. Please assist.

XML:-
<?xml version="1.0" encoding="utf-16"?>
<feed xmlns:es="">
     <Title>XSL</Title>
       <Updated>2010-2-23T12:00:00Z</Updated>
        <Author><Name>UCM</Name></Author>
        <element>
            <Link></Link>
            <Id>001</Id>
            <Updated>2010-2-23T12:00:00Z</Updated>
            <es:RepositoryDetails>
                <es:Repository Type="Test" />
            </es:RepositoryDetails>
            <es:DocProperties>
                <es:DocProperty propertyName="Title">
                    <es:PropValues>
                        <es:PropValue>22172</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
            </es:DocProperties>
        </element>
 </feed>

XSL:-

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
	xmlns="http://www.w3.org/2005/Atom"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:es=""
    exclude-result-prefixes="java wcm">

<xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>	
<xsl:param name="relatedDocumentsXML" /> <!-- in this variable i am getting the XML-->
<xsl:template match="/">
<xsl:apply-templates select="$relatedDocumentsXML/feed" />
</xsl:template>

<xsl:template match="feed">

<xsl:for-each select="element">
	<xsl:apply-templates select="."></xsl:apply-templates>
		</xsl:for-each>
</xsl:template>

<xsl:template match="element">
<xsl:for-each select="Id">				<xsl:value-of select="." /> <!-- i am getting the value for this ID-->
</xsl:for-each>
<xsl:for-each select="es:RepositoryDetails/es:Repository">
		<xsl:value-of select="@Type" /> <!-- not able to get the value for this. Seems like an issue with namespace. Please assist-->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

TIA

Dani AI

Generated

Two things are most likely at work here: the XML being passed into the stylesheet is not being seen as a node-tree by the XSLT processor (so namespace-qualified child nodes are not addressable), or the stylesheet is not matching the same namespace URI used in the source. was on the right track asking whether the parameter is actually a node tree; solved it by stripping namespaces, which works but is brittle.

Quick checks to run first:

  • Print the first child name and its namespace to confirm what the stylesheet actually receives. For example:

    <xsl:value-of select="name($param/*[1])"/>
    <xsl:value-of select="namespace-uri($param/*[1])"/>
  • Verify the stylesheet declares a prefix bound to the same namespace URI used by the source. Prefix names can differ; the URI must match exactly.

If the parameter is a result-tree-fragment or a serialized string (common in XSLT 1.0), convert/parse it before selecting namespaced nodes. Typical fixes:

  • XSLT 1.0 + EXSLT: convert an RTF to a node-set and then select namespaced elements:

    xmlns:exsl="http://exslt.org/common"
    <xsl:variable name="nodes" select="exsl:node-set($param)"/>
    <xsl:apply-templates select="$nodes//es:Repository"/>
  • XSLT 2.0+: parse a string into nodes with the processor function (for example, parse-xml()), then query the resulting tree.

If you cannot change how the XML arrives, use a namespace-agnostic selector as a last resort:

select="$nodes//*[local-name() = 'Repository' and namespace-uri() = 'http://ucmservice']"

Caveat: removing namespaces loses semantic guarantees and can break other processing. Prefer ensuring the parameter is passed as a node tree or parsing it in XSLT, and make sure the stylesheet’s prefix is bound to the exact namespace URI present in the source.

Recommended Answers

All 2 Replies

Hi,
First sorry for my poor english.


Your XSLT is good.
I only have one problem when I make test with

exclude-result-prefixes="java wcm"

Xmlspy don't like but i don't think it's the error.

I must make one change for testing

<xsl:param name="relatedDocumentsXML" />

by

<xsl:param name="relatedDocumentsXML" select="/*" />

May be your problems it's here.
Are you sure that your xml is well given in parameter ?

Thanks Erwan,
I'll try this.

I got the desired result, surprisingly by removing the namespace at the time of calling the tag. :)

Thanks once again.

Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.