Nathon
Nathon

Reputation: 165

How to identify/Check the node exist or not in xml using XSLT, If exist modify the xml and print?

This is sample input xml file i am using in xslt , how to check the particular nodes in the file , if it is present need to print True or false

</persons>        
      '     <?xml version="1.0" ?>
       <persons>
        <person username="JS1">
         <name>John</name>
         <family-name>Smith</family-name>
        </person>
      <person username="MI1">
      <name>Morka</name>
      <family-name>Ismincius</family-name>
       </person>
     </persons>     

Above sample xml file i Need to check <name> node present or not. If name node present it should be print in the output using XSLT.

<?xml version="1.0" ?>
   <persons>
  <person username="JS1">
    <name>John</name>
    <family-name>Smith</family-name>
  </person>
  <person username="MI1">
    <name>Morka</name>
    <family-name>Ismincius</family-name>
  </person>
    <person>
   **<name>True</name>**
   </person>
</persons> 

Upvotes: 1

Views: 523

Answers (1)

Michael Kay
Michael Kay

Reputation: 163587

If the context item is a <person> element, then

<xsl:copy-of select="name"/>

will output the name element if it exists, and will do nothing if it does not exist.

If you want to output <name>true</name> if and only if there is at least one <name> element in the input, you can use

<xsl:if test="//name">
  <name>true</name>
</xsl:if>

If your requirement is different from that, then you will have to explain it much more clearly.

Upvotes: 0

Related Questions