0

I have xml like this:

<?xml version="1.0" encoding="utf-8"?>
<FormDataRequest>   
  <Name>Bob</Name>
  <sn>Ross</sn>
  <company>MS Paint</company>
  <department>Paint bucket</department>
</FormDataRequest>

I should change value of <company> and <department> based on when text is condition.

I have tried with this xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
    <xsl:apply-templates select="FormDataRequest" />
</xsl:template>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>
<xsl:template match="FormDataRequest">
    <xsl:variable name="realcompany">
        <xsl:choose>
            <xsl:when test="./department/text() = 'Paint bucket'">
                <xsl:text>Co company 1</xsl:text>
            </xsl:when>
            <xsl:when test="./department/text() = 'Paint brush'">
                <xsl:text>Co company 2</xsl:text>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="realdepartment">
        <xsl:choose>
            <xsl:when test="./department/text() = 'Paint bucket'">
                <xsl:text>Bucket department</xsl:text>
            </xsl:when>
            <xsl:when test="./department/text() = '´Paint brush'">
                <xsl:text>Brush department</xsl:text>
            </xsl:when>             
        </xsl:choose>
    </xsl:variable>
    <FormDataRequest>
        <xsl:apply-templates select="@*|node()" />
        <company>
            <xsl:value-of select="$realcompany"/>
        </company>
        <department>
            <xsl:value-of select="$realdepartment"/>
        </department>
    </FormDataRequest>
</xsl:template>
</xsl:stylesheet>

Output:

<FormDataRequest>   
  <Name>Bob</Name>
  <sn>Ross</sn>
  <company>MS Paint</company>
  <department>Paint bucket</department>
 <company>Co company 1</company>
 <department>Bucket department</department>
</FormDataRequest>

I do not want to keep old company and department values.

So out put should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<FormDataRequest>   
  <Name>Bob</Name>
  <sn>Ross</sn>
  <company>Co company 1</company>
  <department>Bucket department</department>
</FormDataRequest>
lunkerson
  • 3
  • 2

2 Answers2

0

Well, your <xsl:apply-templates select="@*|node()" /> inside of the FormDataRequest result element processes the existing child nodes and given your use of the identity transformation copies them. If you want to exclude the two elements then, to suggest one change for your existing code, change <xsl:apply-templates select="@*|node()" /> to <xsl:apply-templates select="@*|node() except (company, department)" />.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

With your XSL code you have been quite close, but more easier and readable way is to create replace dynamic template which will help you to change company and department block values regarding your condition in variables, please see XSL below:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <!--declare condition for company block-->
    <xsl:variable name="realcompany">
        <xsl:choose>
            <xsl:when test="//department = 'Paint bucket'">
                <xsl:value-of select="'Co company 1'"/>
            </xsl:when>
            <xsl:when test="//department = 'Paint brush'">
                <xsl:value-of select="'Co company 2'"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="//company"/>
            </xsl:otherwise>
        </xsl:choose>        
    </xsl:variable>
    <!--declare condition for department block-->
    <xsl:variable name="realdepartment">
        <xsl:choose>
            <xsl:when test="//department = 'Paint bucket'">
                <xsl:value-of select="'Bucket department'"/>
            </xsl:when>
            <xsl:when test="//department = 'Paint brush'">
                <xsl:value-of select="'Brush department'"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="//department"/>
            </xsl:otherwise>
        </xsl:choose>         
    </xsl:variable>
    <!--dynamic template for replacing current value to condition value-->    
    <xsl:template name="value-to-replace">
        <xsl:param name="param.str"/>
        <xsl:param name="param.target"/>
        <xsl:param name="param.replacement"/>
        <xsl:choose>
            <xsl:when test="contains($param.str, $param.target)">                    
                <xsl:value-of select="concat(substring-before($param.str, $param.target), $param.replacement, substring-after($param.str, $param.target))"/>              
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$param.str"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!--replace value in company block with realcompany variable condition-->
    <xsl:template match="company[parent::FormDataRequest]">
        <xsl:element name="{name()}">                                    
            <xsl:call-template name="value-to-replace">                       
                <xsl:with-param name="param.str" select="."/>                       
                <xsl:with-param name="param.target" select="//company"/>                       
                <xsl:with-param name="param.replacement" select="$realcompany"/>                   
            </xsl:call-template>
        </xsl:element>                      
    </xsl:template>
    <!--replace value in department block with realdepartment variable condition-->
    <xsl:template match="department[parent::FormDataRequest]">
        <xsl:element name="{name()}">                                    
            <xsl:call-template name="value-to-replace">                       
                <xsl:with-param name="param.str" select="."/>                       
                <xsl:with-param name="param.target" select="//company"/>                       
                <xsl:with-param name="param.replacement" select="$realdepartment"/>                   
            </xsl:call-template>
        </xsl:element>                      
    </xsl:template>
    <!--copy all nodes-->    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>    
</xsl:stylesheet>

Result will be as expected:

<FormDataRequest>   
  <Name>Bob</Name>
  <sn>Ross</sn>
  <company>Co company 1</company>
  <department>Paint bucket</department>
</FormDataRequest>
Alex Fomin
  • 485
  • 4
  • 7