• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is exactly happening here?

 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a stylesheet applied to an xml file. Can someone explain me clearly what is happening here. I'm a little confused. When i say
<xsl:template match="//city">,
i guess it selects all the city elements in the stylesheet.
===============================================
<?xml version="1.0" encoding="utf-8"?>
<country attr="100">USA
<state attr="200">Massachusetts
<city>Boston
<neighborhood>Back Bay</neighborhood>
<neighborhood>Roxbury</neighborhood>
<neighborhood>Jamaica Plain</neighborhood>
</city>
<city>Cambridge</city>
<city>Somerville</city>
<city>Northampton</city>
<city>Lowell</city>
</state>
<state>California
<city>Los Angeles
<neighborhood>Venice</neighborhood>
<neighborhood>Watts</neighborhood>
<neighborhood>Hollywood</neighborhood>
</city>
<city>Santa Monica</city>
<city>West Hollywood</city>
<city>Beverly Hiils</city>
</state>
</country>
===============================================
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:template match="text()" />

<xsl:template match="//city[position()=1]">
<xsl:value-of select="text()"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
================================================
Output --
<?xml version="1.0" encoding="UTF-8"?>
Boston
Los Angeles

=================================================
I guess there is a difference b/w
select="//city" and match="//city".
If so, does it mean that 'match' matches things in context?? and it something like select=".//city"??
Can someone please explain the reasons.
Thanks.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"If so, does it mean that 'match' matches things in context?? "
Yes, exactly and default to child nodes of the context node.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roseanne for the clarification.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<xsl:template match="//city[position()=1]">
This means: match any node called "city", anywhere in the hierarchy, if it is the first child of its parent.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic