Strippin an element in xml and replacing the value of an element based on certain condition using xslt -


i getting stuck @ point need remove element input xml:

<message      xmlns="http://www.origoservices.com"      xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" > <m_control>     <control_timestamp>2013-06-06t14:55:37</control_timestamp>     <initiator_id>asl</initiator_id> </m_control> <m_content>     <b_control>         <quote_type>single company</quote_type>         <quote_or_print>quote , print</quote_or_print>         <generic_quote_ind>yes</generic_quote_ind>         <tpsdata>             <tps_quote_type>comparison</tps_quote_type>         </tpsdata>     </b_control>     <application>      <product>         <tpsdata>             <service_type>quickquote</service_type>             <quote_type>standard</quote_type>         </tpsdata>       </product>     </application> </m_content> </message> 

if <tps_quote_type> 'comparison' change value of <quote_type> 'comparison' , <tpsdata> field should removed. output should below.

<message      xmlns="http://www.origoservices.com"      xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" > <m_control>      <control_timestamp>2013-06-06t14:55:37</control_timestamp>      <initiator_id>asl</initiator_id> </m_control> <m_content>     <b_control>          <quote_type>comparison</quote_type>          <quote_or_print>quote , print</quote_or_print>          <generic_quote_ind>yes</generic_quote_ind>     </b_control>    <application>        <product>           <tpsdata>             <service_type>quickquote</service_type>             <quote_type>standard</quote_type>           </tpsdata>     </product>     </application> </m_content> </message> 

so far have tried xslt, don't know how remove <tpsdata> field output. me in this?

<xsl:stylesheet      version="1.0"      xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:dp="http://www.datapower.com/extensions"     xmlns:fn="http://www.w3.org/2005/xpath-functions"     xmlns:date="http://exslt.org/dates-and-times"      extension-element-prefixes="dp" >     <xsl:output method="xml" indent="yes"/>      <xsl:template match="*">         <!-- identity closing tags -->         <xsl:element name="{name()}">             <xsl:apply-templates select="@*|node()"/>         </xsl:element>     </xsl:template>      <xsl:variable name="quotetype">        <xsl:value-of select="/*[namespace-uri()='http://www.origoservices.com' ,  local- name()='message']/*[namespace-uri()='http://www.origoservices.com' , local-name() ='m_content']/*[namespace-uri()='http://www.origoservices.com' , local-name()='b_control']/*[namespace-uri()='http://www.origoservices.com' , local-name()='quote_type']"/>     </xsl:variable>      <xsl:variable name="tpsquotetype">         <xsl:value-of select="/*[namespace-uri()='http://www.origoservices.com' , local-name()='message']/*[namespace-uri()='http://www.origoservices.com' , local-name()='m_content']/*[namespace-uri()='http://www.origoservices.com' , local-name()='b_control']/*[namespace-uri()='http://www.origoservices.com' , local-name()='tpsdata']/*[namespace-uri()='http://www.origoservices.com' , local-name()='tps_quote_type']"/>     </xsl:variable>      <xsl:template match="/*[namespace-uri()='http://www.origoservices.com' , local-name()='message']/*[namespace-uri()='http://www.origoservices.com' , local-name()='m_content']/*[namespace-uri()='http://www.origoservices.com' , local-name()='b_control']/*[namespace-uri()='http://www.origoservices.com' , local-name()='quote_type']">         <xsl:choose>             <xsl:when test="$tpsquotetype = 'comparison' ">                 <xsl:copy>                     <xsl:copy-of select="@*"/>                     <xsl:text>comparison</xsl:text>                 </xsl:copy>             </xsl:when>             <xsl:otherwise>                 <xsl:copy>                     <xsl:apply-templates select="@*|node()"/>                 </xsl:copy>             </xsl:otherwise>         </xsl:choose>     </xsl:template>      <xsl:template match="*|comment()|processing-instruction()">         <xsl:copy>             <xsl:copy-of select="@*|namespace::*"/>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template>  </xsl:stylesheet> 

maybe noticed handling of elements namespace little painful. add http://www.origoservices.com namespace xslt , pain goes away.

<xsl:stylesheet      version="1.0"      xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:o="http://www.origoservices.com"      xmlns:dp="http://www.datapower.com/extensions"     xmlns:fn="http://www.w3.org/2005/xpath-functions"     xmlns:date="http://exslt.org/dates-and-times"      extension-element-prefixes="dp"     exclude-result-prefixes="fn date" >     <xsl:output method="xml" indent="yes"/>      <xsl:template match="node() | @*">         <xsl:copy>             <xsl:apply-templates select="node() | @*"/>         </xsl:copy>     </xsl:template>      <xsl:template match="o:b_control/o:quote_type[../o:tpsdata/o:tps_quote_type = 'comparison']">         <xsl:copy>             <xsl:apply-templates select="@*" />             <xsl:text>comparison</xsl:text>         </xsl:copy>     </xsl:template>      <xsl:template match="o:tpsdata[o:tps_quote_type = 'comparison']" />  </xsl:stylesheet> 

notes

  • most of "plumbing" not necessary.
  • template match expressions don't need full path.
  • use match expressions rather <xsl:choose> pinpoint elements want change.
  • start basic identity template, overriding needed more specific templates. makes live easier starting modified identity template.
  • use empty templates remove specific elements.

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -