Search code examples
xmlxsltxsdwsdlmule

Multiple <xsl:template match in .xslt file in mule


I use these codes for hiding my methods in my XML(WSDL) file:

    <xsl:template match="wsdl:operation[@name = 'GetCityWeatherByZIP']" />  
<xsl:template match="wsdl:message[@name = 'GetCityWeatherByZIPSoapIn']" /> 
<xsl:template match="wsdl:message[@name = 'GetCityWeatherByZIPSoapOut']" /> 

<xsl:template match="wsdl:message[@name = 'GetCityWeatherByZIPHttpGetIn']" /> 
<xsl:template match="wsdl:message[@name = 'GetCityWeatherByZIPHttpGetOut']" /> 

<xsl:template match="wsdl:message[@name = 'GetCityWeatherByZIPHttpPostIn']" /> 
<xsl:template match="wsdl:message[@name = 'GetCityWeatherByZIPHttpPostOut']" /> 

<xsl:template match="s:element[@name = 'GetCityWeatherByZIP']" /> 
<xsl:template match="s:element[@name = 'GetCityWeatherByZIPResponse']" />

Now I want to use just a condition for hiding all of them instead of multiple if. Actually I use this some lines in my .xslt file for hiding my method from an special ip-address:

    <xsl:template match="wsdl:operation[@name = 'GetCityWeatherByZIP']">
   <xsl:variable name="rcaTrimmed" 
          select="substring-before(substring-after($remoteClientAddress, '/'), ':')" />
   <xsl:if test="not($rcaTrimmed = '192.168.3.74')">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:if>
</xsl:template>

I want to add all of my **<xsl:template match** in a line and then hide all of them from an special ip-address, I dont't want to write for all of my methods these codes one by one. How can I do this purpose?


Solution

  • Do you just want to hide everything that has a @name that starts with 'GetCityWeatherByZIP'? If so, you can just use this single template:

    <xsl:template match="*[starts-with(@name, 'GetCityWeatherByZIP')]">
       <xsl:variable name="rcaTrimmed" 
              select="substring-before(substring-after($remoteClientAddress, '/'), ':')" />
       <xsl:if test="not($rcaTrimmed = '192.168.3.74')">
          <xsl:copy>
             <xsl:apply-templates select="@* | node()" />
          </xsl:copy>
       </xsl:if>
    </xsl:template>
    

    I'd also suggest moving the definition of the rcaTrimmed variable outside of the template so it only needs to be computed once:

    <xsl:variable name="rcaTrimmed" 
              select="substring-before(substring-after($remoteClientAddress, '/'), ':')" />
    
    <xsl:template match="*[starts-with(@name, 'GetCityWeatherByZIP')]">
       <xsl:if test="not($rcaTrimmed = '192.168.3.74')">
          <xsl:copy>
             <xsl:apply-templates select="@* | node()" />
          </xsl:copy>
       </xsl:if>
    </xsl:template>