on 01-05-2012 9:58 AM
Hi Friends,
We have a tricky requirement as follows:
<input>
<item>
<code>A</code>
<value>1000</value>
<mode>A1000</mode>
</item>
<item>
<code>A</code>
<value>2000</value>
<mode>A2000</mode>
</item>
<item>
<code>B</code>
<type>Btype</type>
</item>
<item>
<code>C</code>
<value>3000</value>
<mode>C3000</mode>
<type>Ctype</type>
</item>
</input>
needed output structure;
<output>
<CodeA>
<item>
<name>A</name>
<value>1000</value>
<mode>A1000</mode>
</item>
<item>
<name>A</name>
<value>2000</value>
<mode>A2000</mode>
</item>
</CodeA>
<CodeB>
<item>
<name>B</name>
<type>Btype</type>
</item>
</CodeB>
<CodeC>
<item>
<name>C</name>
<value>3000</value>
<mode>C3000</mode>
<type>Ctype</type>
</item>
</CodeC>
</output>
Can anyone please guide me?
Thanks,
Peter
Hi Peter,
you can use a for-each with condition:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<CodeA>
<xsl:for-each select="item[./code='A']">
<item>
<name>A</name>
<value><xsl:value-of select="value"></xsl:value-of></value>
<mode><xsl:value-of select="mode"></xsl:value-of></mode>
</item>
</xsl:for-each>
</CodeA>
</xsl:template>
</xsl:stylesheet>
Regards,
Udo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
thanks for the prompt reply. but what happened when <code>A</code> doesnt exist? For each is coming after the <CodeA> Segment in XSLT. The Output is then like
<Output>
<CodeA></CodeA>
<CodeB>
<item>
...
...
</CodeB>
<CodeA> sholdnt be created in Output. Any ideas?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<CodeA>
<xsl:for-each select="item[./code='A']">
<item>
<name>A</name>
<value><xsl:value-of select="value"></xsl:value-of></value>
<mode><xsl:value-of select="mode"></xsl:value-of></mode>
</item>
</xsl:for-each>
</CodeA>
</xsl:template>
</xsl:stylesheet>
Hi Peter,
you put a condition around:
<?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:if test="//item/code = 'A' ">
<CodeA>
<xsl:for-each select="item[./code='A']">
<item>
<name>A</name>
<value>
<xsl:value-of select="value"/>
</value>
<mode>
<xsl:value-of select="mode"/>
</mode>
</item>
</xsl:for-each>
</CodeA>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Regards,
Udo
Hi Peter,
Here is the XSLT mapping as per your requirement.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<output>
<xsl:if test=".//code = 'A' ">
<CodeA>
<xsl:for-each select="input/item[code='A']">
<item>
<name>A</name>
<value><xsl:value-of select="./value"></xsl:value-of></value>
<mode><xsl:value-of select="./mode"></xsl:value-of></mode>
</item>
</xsl:for-each>
</CodeA>
</xsl:if>
<xsl:if test=".//code = 'B' ">
<CodeB>
<xsl:for-each select="input/item[code='B']">
<item>
<name>B</name>
<type><xsl:value-of select="./type"></xsl:value-of></type>
</item>
</xsl:for-each>
</CodeB>
</xsl:if>
<xsl:if test=".//code = 'C' ">
<CodeC>
<xsl:for-each select="input/item[code='C']">
<item>
<name>C</name>
<value><xsl:value-of select="./value"></xsl:value-of></value>
<mode><xsl:value-of select="./mode"></xsl:value-of></mode>
<type><xsl:value-of select="./type"></xsl:value-of></type>
</item>
</xsl:for-each>
</CodeC>
</xsl:if>
</output>
</xsl:template>
</xsl:stylesheet>
Please change encoding if you require.
Hope this solves your problem.
Just included "If" condition as per your requirement.
Regards
Anupam
Edited by: anupamsap on Jan 5, 2012 2:21 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Make the minimum occurrence of type and value elements to zero & try with below graphical mapping...
code>removecontext>sort>splitByvaluechange>item
code>removecontext>sort>splitByEachValue>name
formatByExample>if equals CONST then replace with NULL which will be suppressed in the target> value
formatByExample:
input1:resultof sortby key
input2:code>removecontext>sort-->plitByEachValue
sortByKey:
input1:code-->removecontext
input2:value>mapwith default (CONST)>removecontext-->sort
apply the same logic to all other target elements.
Regards,
Priyanka
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.