1

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>
VJ THAKUR
  • 91
  • 2
  • 8
  • 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 May 04 '16 at 16:19
  • Your question is not clear: "*if child = 1*" Which child? The first one? Or any child? – michael.hor257k May 04 '16 at 16:20
  • Have a look here: http://stackoverflow.com/questions/7089712/how-to-control-boolean-rendering-in-xslt You forgot to add `xsl:output omit-xml-declaration="yes" indent="yes"/>` declaration. – Maciej Los May 04 '16 at 16:23
  • No need to update the output -- just create correct output, as shown in my answer :). +1 – Dimitre Novatchev May 05 '16 at 06:07
  • 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 May 05 '16 at 10:27
  • @VJTHAKUR Is your question not answered? If not, please explain why not. – michael.hor257k May 05 '16 at 10:33
  • @michael.hor257k the answer which you provided it's working fine for the input 1 2 but i'm not expecting a duplicate tags in my xml. Please can you check the updated input xml and xslt and provide me the solution for the same thanks. – VJ THAKUR May 05 '16 at 10:50
  • Well, yes: that was the original question, wasn't it? Can't you make the adjustment to your new input? – michael.hor257k May 05 '16 at 10:52

2 Answers2

1

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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

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>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431