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>
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. Commented May 4, 2016 at 16:19
  • Your question is not clear: "if child = 1" Which child? The first one? Or any child? Commented May 4, 2016 at 16:20
  • Have a look here: stackoverflow.com/questions/7089712/… You forgot to add xsl:output omit-xml-declaration="yes" indent="yes"/> declaration. Commented May 4, 2016 at 16:23
  • No need to update the output -- just create correct output, as shown in my answer :). +1 Commented May 5, 2016 at 6: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. Commented May 5, 2016 at 10:27

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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>

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.