Srikrishna Pothukuchi
Srikrishna Pothukuchi

Reputation: 41

Dealing with namespaces and variables in XSLT

I have an xml file like the below. For brevity I am not listing it complete.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<OBJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns="http://www.example.com/"
         xsi:schemaLocation=".\\intermediate\\example.xsd">
   <OBJECT>
      <abbreviation>ABCD</abbreviation>
      <LINKS>
      ....

And I want to refer some parts of it in my XSLT.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:t="http://www.example.com/" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation=".\\intermediate\\example.xsd">

 <xsl:output method="xml" indent="yes" encoding="utf-8"/>
 <xsl:strip-space elements="*"/>

<xsl:variable name="objs" select ="t:OBJECTS/OBJECT"/>
<xsl:variable name="cnt" select="count($objs)"/> 

<xsl:template match="t:*">
Count of objects is <xsl:value-of select="$cnt" />
</xsl:template>

</xsl:stylesheet>

My intention is to display count of all objects as per that namespace. I could not achieve this.

Count of objects is 100.

If I remove namespace in the XML file, I am getting it. Where would be the problem?

Upvotes: 0

Views: 578

Answers (1)

imhotap
imhotap

Reputation: 2490

Not 100% sure what you want to achieve but I've noticed you're not using the t: prefix in your child selector, so I'm suggesting to use

<xsl:variable name="objs" select ="t:OBJECTS/t:OBJECT"/>

instead of

<xsl:variable name="objs" select ="t:OBJECTS/OBJECT"/>

Upvotes: 2

Related Questions