0

In context to my earlier question at "XSLT transformation from XML to XML document". Further i was trying to rename the "<value>" element to "<sName>" along with updating the value to "ABC" if the value of "dataset/type/text() ='test'". I wrote below code for the same but it was not working as expected. please help.

Source XML-

<soapenv:Header/>
    <soapenv:Body>
        <v1:QueryRequest version="1">
            <subject>
            <dataList>
                <!--1 or more repetitions:-->
                <dataset>
                <type>company</type>
                <value>abc</value>
            </dataset>
            <dataset>
                <type>user</type>
                <value>xyz</value>
            </dataset>
        </dataList>
        </subject>      
        <testList>
        <!--1 or more repetitions:-->
            <criteria>
                    <type>test</type>
                    <value>1234</value>
            </criteria>
            <criteria>
        <type>test2</type>
        <value>false</value>
            </criteria>
        </testList>
        </v1:QueryRequest>
    </soapenv:Body>
</soapenv:Envelope>

XSL file :-

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="http://www.abc.com/s/v1.0"
exclude-result-prefixes="old"
xmlns:v2="http://www.abc.com/s/v2.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"
    version="1.0" />

<xsl:param name="newversion" select="'2.0'" />
<xsl:param name="ABC" select="'ABC'" />
<xsl:param name="XYZ" select="'XYZ'" />
<xsl:param name="DFG" select="'DFG'" />


<!-- fix namespace declarations on root element -->
<xsl:template match="/*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
        <!-- copy the v2: binding from the stylesheet document -->
        <xsl:copy-of select="document('')/xsl:stylesheet/namespace::v2" />
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

<!-- replace namespace of elements in old namespace -->
<xsl:template match="old:*">
    <xsl:element name="v2:{local-name()}">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

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

<xsl:template match="old:QueryRequest/@version">
    <xsl:attribute name="version">
      <xsl:value-of select="$newversion" />
     </xsl:attribute>
</xsl:template>


 <xsl:template match="criteria[type='service']/value">
         <xsl:choose>
         <xsl:when test="criteria[type='service']/value/text()='1234'">
        <xsl:element name="sName">
            <xsl:value-of select="$ABC"/>           
            </xsl:element>
        </xsl:when>
        <xsl:when test="criteria[type='service']/value/text()='5464'">
        <xsl:element name="sName">
            <xsl:value-of select="$XYZ"/>
        </xsl:element>                      
        </xsl:when>       
        <xsl:when test="criteria[type='service']/value/text()='8755'">
        <xsl:element name="sName">
            <xsl:value-of select="$DFG"/>
        </xsl:element>          
        </xsl:when>                     
       </xsl:choose>
     </xsl:template>


    <xsl:template match="type/text()">
        <xsl:value-of
        select="translate(., 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
    </xsl:template>

</xsl:stylesheet>

Expected Output:

   <testList>
    <criteria>
        <type>test</type>
        <sName>ABC</sName>
    </criteria>
    <criteria>
        <type>test2</type>
        <value>false</value>
    </criteria>
</testList>

1 Answer 1

2

Within a

<xsl:template match="criteria[type='service']/value">

the value element is the current context node, so your other XPath expressions need to be relative to that, i.e. instead of

<xsl:when test="criteria[type='service']/value/text()='1234'">

you would just use

<xsl:when test="text()='1234'">

or better

<xsl:when test=".='1234'">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.