Home Page › Forums › BizTalk 2004 – BizTalk 2010 › Remove record based on data in an inner loop
- This topic has 4 replies, 1 voice, and was last updated 8 years, 10 months ago by
community-content.
-
AuthorPosts
-
-
December 7, 2009 at 2:01 AM #23850
Hello,
I’m trying to remove records from an xml file with structure as below.
I need to remove every “data-structure1”-record that contains a value2 that has a specific value.
Both “data-structure1” and “data-structure2” occurrs several times.<xml>
<head-value/>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>
</data-structure2>
</data-structure1>
</xml>XSLT doesn’t work since I can’t keep a value outside a for-each loop. The only solution I’ve come up with is splitting the message in “data-structure1”-messages and then merge them together again after checking the contents of each message. This seems a bit tedious though.
Help anybody?
Kind regards
Charles
-
December 7, 2009 at 4:26 AM #23851
Not sure if I fully understand your problem but here goes:
With this input file
<root>
<head-value/>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>27</value2>
</data-structure2>
</data-structure1>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>31</value2>
</data-structure2>
</data-structure1>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>31</value2>
</data-structure2>
</data-structure1>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>29</value2>
</data-structure2>
</data-structure1>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>27</value2>
</data-structure2>
</data-structure1>
</root>This Xslt will only map data-structure1 records where value2 is not equal to 27
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:msxsl=”urn:schemas-microsoft-com:xslt” xmlns:var=”http://schemas.microsoft.com/BizTalk/2003/var” exclude-result-prefixes=”msxsl var” version=”1.0″>
<xsl:output omit-xml-declaration=”yes” method=”xml” version=”1.0″ />
<xsl:template match=”/”>
<xsl:apply-templates select=”/root” />
</xsl:template>
<xsl:template match=”/root”>
<xsl:element name=”root”>
<xsl:element name=”head-value”/>
<xsl:for-each select=”data-structure1[data-structure2/value2/text() != ’27’]”>
<xsl:copy-of select=”.”/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>-
December 7, 2009 at 8:15 AM #23852
Thank You for your answer, obviously I was a bit unclear. If I use your example with a bit less “data-structure1” (see below) but several value2 per “data-structure2” instead:
<root>
<head-value/>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>27</value2>
<value2>28</value2>
<value2>29</value2>
</data-structure2>
</data-structure1>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>30</value2>
<value2>31</value2>
<value2>32</value2>
</data-structure2>
</data-structure1>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>22</value2>
<value2>23</value2>
<value2>27</value2>
</data-structure2>
</data-structure1>
</root>In this example I’d like the output to be this if we are to remove all the “data-structure1″s that contains a “data-structure1->data-structure2->value2 = 27:
<root>
<head-value/>
<data-structure1>
<value1>value</value1>
<data-structure2>
<value2>30</value2>
<value2>31</value2>
<value2>32</value2>
</data-structure2>
</data-structure1>
</root>My problem is that there exists “two loops” that need to be taken care of. I’m not set to do this with xslt but it would feel good to be able to do it there.
Thanks in advance,
Charles
-
December 7, 2009 at 11:39 AM #23853
You can use the count function to check for the existence of child nodes with a particular value:
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:msxsl=”urn:schemas-microsoft-com:xslt” xmlns:var=”http://schemas.microsoft.com/BizTalk/2003/var” exclude-result-prefixes=”msxsl var” version=”1.0″>
<xsl:output omit-xml-declaration=”yes” method=”xml” version=”1.0″ />
<xsl:template match=”/”>
<xsl:apply-templates select=”/root” />
</xsl:template>
<xsl:template match=”/root”>
<xsl:element name=”root”>
<xsl:element name=”head-value”/>
<xsl:for-each select=”data-structure1[count(data-structure2/value2[text() = ’27’]) = 0]”>
<xsl:copy-of select=”.”/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>-
December 8, 2009 at 2:53 AM #23858
Thank you very much 🙂
Works like a charm.
/Charles
-
-
-
-
-
AuthorPosts
- The forum ‘BizTalk 2004 – BizTalk 2010’ is closed to new topics and replies.