1
votes

I want to generate a xml file by using xslt version 1.0. But I'm not able to update the element value of xml node in output file Eg.

Input xml file

 <Node>
   <product>
     <productSelected>false</productSelected>       
     <productId>L0001</productId>
  </product>
  <product>
     <productSelected>true</productSelected>       
     <productId>L0002</productId>
  </product>
  <product>
     <productSelected>true</productSelected>       
     <productId>L0003</productId>
  </product>
  <product>
     <productSelected>false</productSelected>       
     <productId>L0004</productId>
  </product>
</Node>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
   <xsl:apply-templates select="Node/product"/>
</xsl:template>

<xsl:template match="Node/parent/productSelected">
 <xsl:choose>
  <xsl:when test=". = 'true'">
     <xsl:element name="status">
       <xsl:value-of select="true()" />
    </xsl:element>   
  </xsl:when>

</xsl:template>
</xsl:stylesheet>

Output xml

<status>true</status>
<status>true</status>

In output, there is a two nodes with same name I'm just expecting a single node instead of two duplicate nodes Eg. Output should be

<status>true</status>
2
Not clear. Seems, you want to replace all child nodes with status and update their values to boolean. Am i right? What should be as result. Provide entire output xml.Maciej Los
Your question is not clear: "if child = 1" Which child? The first one? Or any child?michael.hor257k
Have a look here: stackoverflow.com/questions/7089712/… You forgot to add xsl:output omit-xml-declaration="yes" indent="yes"/> declaration.Maciej Los
No need to update the output -- just create correct output, as shown in my answer :). +1Dimitre Novatchev
Maciej Los: I don't have intention of changing the existing input xml tags. My expectation is to avoid the redundant tags from the output xml. I have updated the input xml and output xml for more understanding.VJ THAKUR

2 Answers

1
votes

I am guessing (!) you want to do:

<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="/Node">
    <status>
        <xsl:value-of select="boolean(parent[child=1])" />
    </status>
</xsl:template>

</xsl:stylesheet>

This will return:

<?xml version="1.0" encoding="UTF-8"?>
<status>true</status>

if there is at least one child element with the value of 1. Otherwise the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<status>false</status>
0
votes

Just use:

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

  <xsl:template match="parent">
    <status><xsl:value-of select="child[. = 1] = 1"/></status>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<Node>
    <parent>
        <child>1</child>
        <child>2</child>
    </parent>
</Node>

the wanted, correct result is produced:

<status>true</status>

If the XML document is this:

<Node>
    <parent>
        <child>3</child>
        <child>2</child>
    </parent>
</Node>

then the transformation again produces the wanted, correct result:

<status>false</status>