I have a xml that I need to process that has 1 or 2 child nodes. It will normally have one "heading" node and one "body" node. But sometimes it will have only the "body" node. So I have a match for "heading" that creates a record using both the "heading" and the following-sibling "body" node. This works fine, unless there is only a "body" node. It will ignore the body node altogether: ***************************************************** <?xml version="1.0" encoding="utf-8"?> <ACI-Report> <appraisal> <data> <form name="1025_05" primary="true"> <section type="subject" number="0"> <tag name="CITY.1" flags="1" format="4096">DEARBORN</tag> <tag name="GS_COMMENTS.1" flags="4" format="4096">SEE ADDENDUM <addendum> <heading>Comments on Sales Comparison</heading> <body>VARYING WEIGHT AND CONSIDERATION WAS GIVEN</body> </addendum> </tag> <tag name="GS_GBA_SQFT.1" flags="8" format="4128">2477</tag> <tag name="ADDEND_CMNTS.2" flags="6" format="4096">See Attached Addendum <addendum> <body>IN MICHIGAN</body> </addendum> </tag> </section> </form> </data> </appraisal> </ACI-Report> **************************************************** The xsl is: ******************************************************* <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/*"> <xsl:copy> <xsl:apply-templates select="appraisal/data/form/tag | appraisal/data/form/section/tag"> <xsl:sort select="ancestor::form/@primary" order="descending"/> <xsl:sort select="ancestor::section/@number" order="ascending" data-type="number"/> </xsl:apply-templates> <xsl:apply-templates select="appraisal/attachments/attachment"/> </xsl:copy> </xsl:template> <xsl:template match="tag"> <form> <sectionNumber> <xsl:value-of select="ancestor::section/@number"/> </sectionNumber> <primary> <xsl:value-of select="ancestor::form/@primary"/> </primary> <formName> <xsl:value-of select="ancestor::form/@name"/> </formName> <tagName> <xsl:value-of select="@name"/> </tagName> <flags> <xsl:if test="not(@flags)"> <xsl:attribute name="xsi:nil">true</xsl:attribute> </xsl:if> <xsl:value-of select="@flags"/> </flags> <format> <xsl:if test="not(@format)"> <xsl:attribute name="xsi:nil">true</xsl:attribute> </xsl:if> <xsl:value-of select="@format"/> </format> <value> <xsl:value-of select="text()"/> </value> </form> <xsl:apply-templates select="addendum/heading | addendum/body"/> </xsl:template> <xsl:template match="heading"> <form> <sectionNumber>0</sectionNumber> <primary>false</primary> <formName>addendum</formName> <tagName> <xsl:value-of select="text()"/> </tagName> <flags>0</flags> <format>0</format> <value><xsl:value-of select="following-sibling::body/text()"/></value> </form> </xsl:template> <xsl:template match="body"> <form> <sectionNumber>0</sectionNumber> <primary>false</primary> <formName>addendum</formName> <tagName></tagName> <flags>0</flags> <format>0</format> <value> <xsl:value-of select="text()"/> </value> </form> </xsl:template> <xsl:template match="attachments/attachment"> <attachment> <key> <xsl:value-of select="@key"/> </key> <type> <xsl:value-of select="@type"/> </type> <label> <xsl:value-of select="@label"/> </label> <imageFormat> <xsl:value-of select="image/binary/@format"/> </imageFormat> <imageText> <xsl:value-of select="image/binary/text()"/> </imageText> <thumbnailFormat> <xsl:value-of select="thumbnail/binary/@format"/> </thumbnailFormat> <thumbnailImage> <xsl:value-of select="thumbnail/binary/text()"/> </thumbnailImage> <sourceFormat> <xsl:value-of select="source/@format"/> </sourceFormat> <sourceKey> <xsl:value-of select="source/@key"/> </sourceKey> <sourceText> <xsl:value-of select="source/binary/text()"/> </sourceText> </attachment> </xsl:template> </xsl:stylesheet> ******************************************************* It resuls in the following records: ******************************************************* Row: 0: 0 true 1025_05 CITY.1 1 4096 DEARBORN Row: 1: 0 true 1025_05 GS_COMMENTS.1 4 4096 SEE ADDENDUM Row: 2: 0 false addendum Comments on Sales Comparison 0 0 VARYING WEIGHT AND CONSIDERATION WAS GIVEN Row: 3: 0 false addendum 0 0 VARYING WEIGHT AND CONSIDERATION WAS GIVEN Row: 4: 0 true 1025_05 GS_GBA_SQFT.1 8 4128 2477 Row: 5: 0 true 1025_05 ADDEND_CMNTS.2 6 4096 See Attached Addendum Row: 6: 0 false addendum 0 0 IN MICHIGAN ********************************************************************* You can see that the 1st addendum is done once for the heading and once for the body matches. This is caused by: <xsl:apply-templates select="addendum/heading | addendum/body"/> How do I change this so that it only matches the "body" node when there is no "heading" inside the "addendum" nodes? Thanks, Tom
tshad wrote: > You can see that the 1st addendum is done once for the heading and once for > the body matches. This is caused by: > > <xsl:apply-templates select="addendum/heading | addendum/body"/> > > How do I change this so that it only matches the "body" node when there is > no "heading" inside the "addendum" nodes? Can't you simply use <xsl:apply-templates select="addendum[heading | body]"/> and then have an <xsl:template match="addendum"> that outputs elements you want? That way you process all addendum elements that have at least one headin or body child elements and then in the template for addendum you can put the elements you need. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
"Martin Honnen" wrote in message news:%23cDswRlwIHA.1236@TK2MSFTNGP02.phx.gbl... > tshad wrote: > >> You can see that the 1st addendum is done once for the heading and once >> for the body matches. This is caused by: >> >> <xsl:apply-templates select="addendum/heading | addendum/body"/> >> >> How do I change this so that it only matches the "body" node when there >> is no "heading" inside the "addendum" nodes? > > Can't you simply use > <xsl:apply-templates select="addendum[heading | body]"/> > and then have an > <xsl:template match="addendum"> > that outputs elements you want? > > That way you process all addendum elements that have at least one headin > or body child elements and then in the template for addendum you can put > the elements you need. How easy. You're right. I was so busy looking at the heading as my match I didn't even see that. Thanks, Tom > > > > -- > > Martin Honnen --- MVP XML > http://JavaScript.FAQTs.com/