on 02-10-2017 4:28 PM
If i am getting input like below
<Root>
<row>
<field>A</field>
</row>
<row>
<field>B</field>
</row>
<row>
<field>A</field>
</row>
</Root>
Then i want output like this
<Root>
<row>
<field>B</field>
</row>
</Root>
Is it possible through xslt or xsd validation?
Hi Aditya!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<Root>
<xsl:apply-templates select="//field[not(text()=preceding::text()) and not(text()=following::text())]"/>
</Root>
</xsl:template>
<xsl:template match="field">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Source:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<row>
<field>A</field>
</row>
<row>
<field>B</field>
</row>
<row>
<field>A</field>
</row>
<row>
<field>C</field>
</row>
<row>
<field>D</field>
</row>
<row>
<field>C</field>
</row>
</Root>
Result:
<?xml version="1.0" encoding="utf-8"?><Root><field>B</field><field>D</field></Root>
Regards, Evgeniy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Aditya,
Below will help you to do the same.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="matchCase" match="row" use="field"/>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="text()"/>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Root">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="text()"/>
<xsl:apply-templates select="*[local-name() != 'row']"/>
<xsl:for-each select="row">
<xsl:if test="generate-id(key('matchCase',field)[1]) = generate-id(.)">
<xsl:apply-templates select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Regards,
Sriprasad Shivaram Bhat
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sriprasad,
Thanks for code!!!
By using your code i am getting output :
<Root>
<row>
<field>A</field>
</row>
<row>
<field>B</field>
</row>
</Root>
But i want output like:
<Root>
<row>
<field>B</field>
</row>
</Root>
And is it possible to catch the rest two duplicate filed
<Root>
<row>
<field>A</field>
</row>
<row>
<field>A</field>
</row>
</Root>
Hi Aditya!
Yes, it's possible with XSLT or java mapping. I guess it can be achieved even using graphical mapping.
Regards, Evgeniy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
94 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.