Reputation: 8563
I am quite new to stylesheets, so I am having some elementary problems.
Here is my XML:
<NS1:Envelope xmlns:NS1="http://blahblahblah">
<NS1:Body>
<NS2:STR xmlns:NS2="http://blahblah">
<NS2:STD>
<NS2:ST>CA</NS2:ST>
<NS2:CTY>Los Angeles</NS2:CTY>
<NS2:CY>Artesia</NS2:CY>
<NS2:STGC>05</NS2:STGC>
<NS2:CTYGC>037</NS2:CTYGC>
<NS2:CYGC>0160</NS2:CYGC>
<NS2:GC>050370160</NS2:GC>
<NS2:STTR>0.065000</NS2:STTR>
<NS2:CTYTR>0.025000</NS2:CTYTR>
<NS2:CYTR>0.000000</NS2:CYTR>
<NS2:TotalTR>0.090000</NS2:TotalTR>
<NS2:EffectiveDate>2014-09-24</NS2:EffectiveDate>
</NS2:STD>
<NS2:STD>
<NS2:ST>CA</NS2:ST>
<NS2:CTY>Los Angeles</NS2:CTY>
<NS2:CY>Cerritos</NS2:CY>
<NS2:STGC>05</NS2:STGC>
<NS2:CTYGC>037</NS2:CTYGC>
<NS2:CYGC>6430</NS2:CYGC>
<NS2:GC>050370160</NS2:GC>
<NS2:STTR>0.065000</NS2:STTR>
<NS2:CTYTR>0.025000</NS2:CTYTR>
<NS2:CYTR>0.000000</NS2:CYTR>
<NS2:TotalTR>0.090000</NS2:TotalTR>
<NS2:EffectiveDate>2014-09-24</NS2:EffectiveDate>
</NS2:STD>
</NS2:STR>
</NS1:Body>
</NS1:Envelope>
And here is my stylesheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:NS1="http://blahblahblah"
xmlns:NS2="http://blahblah">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/NS1:Envelope/NS1:Body/NS2:STR/NS2:STD">
<xsl:element name="CALL_ENGINE_RESPONSE">
<xsl:value-of select="NS2:ST" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The response I get is :
<?xml version="1.0" encoding="UTF-8"?>
<CALL_ENGINE_RESPONSE>CA</CALL_ENGINE_RESPONSE>
<CALL_ENGINE_RESPONSE>CA</CALL_ENGINE_RESPONSE>
I am trying to get a response like
<CALL_ENGINE_RESPONSE>
<STATE>CA</STATE>
<STATE>CA</STATE>
</CALL_ENGINE_RESPONSE>
Please let me know if I should edit my formatting, and I apologize for making it so long.
Upvotes: 0
Views: 4482
Reputation: 70618
Your handling of namespaces is perfectly fine!
To solve your problem, what you could do is have a template that matches a common ancestor of your NS2:STD
and output the CALL_ENGINE_RESPONSE
there, before selecting the NS2:STD
element
<xsl:template match="NS2:STR">
<CALL_ENGINE_RESPONSE>
<xsl:apply-templates select="NS2:STD" />
</CALL_ENGINE_RESPONSE>
</xsl:template>
In the template that matches NS2:STD
you would then output the STATE
element
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:NS1="http://blahblahblah" xmlns:NS2="http://blahblah" exclude-result-prefixes="NS1 NS2">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="NS2:STR">
<CALL_ENGINE_RESPONSE>
<xsl:apply-templates select="NS2:STD"/>
</CALL_ENGINE_RESPONSE>
</xsl:template>
<xsl:template match="NS2:STD">
<STATE>
<xsl:value-of select="NS2:ST"/>
</STATE>
</xsl:template>
</xsl:stylesheet>
Note there is no need to put the full path to the NS2:STD
element in the template match. You would only need to do this if there were other NS2:STD
elements at different positions in the hierarchy that you didn't want to match.
Upvotes: 1