tags:

views:

121

answers:

2

I would like an xsl that replaces the value attribute of the data elements only if the relevant param names are passed (they are passed from the calling java program).

Input

<applicationVariables applicationServer="tomcat">
    <data name="HOST" value="localhost"/>
    <data name="PORT" value="8080"/>
    <data name="SIZE" value="1000"/>
</applicationVariables>

So for example if passing in a param HOST1=myHost and PORT=9080 the output should be:

<applicationVariables applicationServer="tomcat">
    <data name="HOST" value="myHost"/>
    <data name="PORT" value="9080"/>
    <data name="SIZE" value="1000"/>
</applicationVariables>

Note that HOST and PORT where replaced but SIZE was not replaced because there was no parameter with name SIZE

I don't want a hardcoded check for each name, as below:

<xsl:when test="not($HOST)"> <!-- parameter has not been supplied -->
    <xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute>
</xsl:when>
<xsl:otherwise> <!--parameter has been supplied -->
    <xsl:attribute name="value"><xsl:value-of select="$HOST"/></xsl:attribute>
</xsl:otherwise>

I want a generic way of saying: replace the value attribute only if a param with the same name exists. But how do i check if a param with name = @name exists?

A: 

You can combine logical conditions with xsl:if.

Assaf Lavie
xsl:if does not solve my problem.I just edited my question to clarify my problem.
Assaf
A: 

In such case it is much better to pass all params as elements of a single <xsl:param>:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pParams">
  <p name="HOST">myHost</p>
  <p name="PORT">9080</p>
 </xsl:param>

 <xsl:variable name="vParams" select=
   "document('')/*/xsl:param[@name='pParams']/*"/>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@value">
  <xsl:attribute name="value">
    <xsl:value-of select=
      "$vParams[@name=current()/../@name]
      |
       current()[not($vParams[@name=current()/../@name])]
      "/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<applicationVariables applicationServer="tomcat">
    <data name="HOST" value="localhost"/>
    <data name="PORT" value="8080"/>
    <data name="SIZE" value="1000"/>
</applicationVariables>

the wanted, correct result is produced:

<applicationVariables applicationServer="tomcat">
    <data name="HOST" value="myHost"></data>
    <data name="PORT" value="9080"></data>
    <data name="SIZE" value="1000"></data>
</applicationVariables>
Dimitre Novatchev
But how do i pass this param from my java program? I tried the following but it does not work***transformer.setParameter("pParams", "<p name=\"HOST1\">myHost</p><p name=\"PORT1\">myHost</p>");***
Assaf
@Assaf: This is implementation dependent and varies among processors. You have to read the documentation of your XSLT processor and find an example there.
Dimitre Novatchev