0

Below is my xml:

<Orderdetails>
<OrderPlace>CDGV</OrderPlace>
<OrderID>1234</OrderID>
<OrderStatus>Active<OrderStatus>
</Orderdetails>

Below is my XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="/">
<xsl:choose> 
 <xsl:when test = "OrderStatus=Active"> 
    <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>             
    <xsl:otherwise> 
    Order Status Inactive
     </xsl:otherwise> 
    </xsl:choose> 
    <xsl:copy-of select="*" />
    </xsl:template>
</xsl:stylesheet>

I want to copy all the elements and values only when the value is 'Active' in the input xml, Otherwise it should display "Order status as Inactive".Can you please let me know where should I make changes in my XSLT ?

michael.hor257k
  • 96,733
  • 5
  • 30
  • 46
user
  • 9
  • 4

1 Answers1

0

I want to copy all the elements and values only when the value is 'Active' in the input xml, Otherwise it should display "Order status as Inactive"

This would be easy to achieve using:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

<xsl:template match="Orderdetails">
    <xsl:choose> 
        <xsl:when test="OrderStatus='Active'"> 
            <xsl:copy-of select="."/> 
        </xsl:when>          
        <xsl:otherwise>Order Status Inactive</xsl:otherwise> 
    </xsl:choose> 
</xsl:template>
  
</xsl:stylesheet>

However this has a flaw: when the test is true, the result will be a copy of the source XML document; when it is false, the result will be a string.

Note also that this assumes that there will be only one Orderdetails element.

michael.hor257k
  • 96,733
  • 5
  • 30
  • 46