Reputation: 3767
I am trying to apply xsl(using apply-template) to a xml,
here is the xml,
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SearchResponse xmlns="http://xyz/abcApi">
<SearchResult>
<Status>
<StatusCode>01</StatusCode>
<Description>Successful</Description>
<Category>SR</Category>
</Status>
<Result>
<WSResult>
<Index>1</Index>
<CityId>111</CityId>
</WSResult>
<WSResult>
<Index>2</Index>
<CityId>111</CityId>
</WSResult>
</Result>
<SessionId>1fc15f22-a670-4f33-b050-c93fa3184cb1</SessionId>
<IsDomestic>true</IsDomestic>
</SearchResult>
</SearchResponse>
</soap:Body>
</soap:Envelope>
and xsl is,
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Response>
<Param>
<Ref>
<Results>
<xsl:apply-templates select="//SearchResponse/SearchResult/Result/WSResult"/>
</Results>
</Ref>
</Param>
</Response>
</xsl:template>
<xsl:template match="WSResult">
<Result>
<Property>
<xsl:attribute name="Id"><xsl:value-of select="position()"/></xsl:attribute>
<xsl:attribute name="CityCode"><xsl:value-of select="CityId"/></xsl:attribute>
</Property>
</Result>
</xsl:template>
</xsl:stylesheet>
this xml is unable to apply specify xsl above. please suggest the solution.
Upvotes: 0
Views: 284
Reputation: 501
This is because you should specify the namespace in your XPath expressions. First you add a namespace in the header (I chose "search" for the prefix, but you can change that). Then you add this prefix to all your element names:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:search="http://xyz/abcApi">
<xsl:template match="/">
<Response>
<Param>
<Ref>
<Results>
<xsl:apply-templates select="//search:SearchResponse/search:SearchResult/search:Result/search:WSResult"/>
</Results>
</Ref>
</Param>
</Response>
</xsl:template>
<xsl:template match="search:WSResult">
<Result>
<Property>
<xsl:attribute name="Id"><xsl:value-of select="position()"/></xsl:attribute>
<xsl:attribute name="CityCode"><xsl:value-of select="search:CityId"/></xsl:attribute>
</Property>
</Result>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1