-1

I will get a fIXML message like below.

<FIXML v="4.4" xsi:schemaLocation="../../schema/fixml-main-4-4.xsd" xmlns="http://www.fixprotocol.org/FIXML-4-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Main attr1="19000" attr2="10">
    <Hdr hattr1="ABC" hattr2="DEF"/>
    <Insert1 I1attr1 ="2008-08" I1attr2 ="20080810"/>
    <Insert2 I2attr1="A111C" I2attr2 ="123">
    <sub ID="1AC"/>
    </Insert2>
    <Insert2 I2attr1="A222C" I2attr2 ="456">
    <sub ID="1BC"/>
    </Insert2>
</Main>
</FIXML>

From this I need to check the mandatory sections are present or not if not present then have to add them by self closing the tag.

After the <Hdr ..> element check <Insert1../> element is there or not, if present keep that section as is, if not present then have to add <Insert1/> self close tag after the <Hdr ..> element, then check for <Insert2.../> if present keep that element after <Insert1 .../> or <Insert1/> if not present add after the <Insert1/>.

To form an xml like this.

<FIXML v="4.4" xmlns="http://www.fixprotocol.org/FIXML-4-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Main attr1="19000" attr2="10">
        <Hdr hattr1="ABC" hattr2="DEF"/>
        <Insert1/>
        <Insert2>
        <sub/>
        </Insert2>
        <Insert2>
        <sub/>
        </Insert2>
    </Main>
    </FIXML>

Please suggest.

Thanks in advance.

subash
  • 13
  • 2
  • 9
  • are you trying to validate the existence of only the first order child nodes present within "Main" node? And more importantly your xml has to a valid xml to xslt transformation, which is not the case – Saurav Jul 30 '15 at 12:48
  • Hi Saurav.Yes correct all under the
    element tag, ok I am showing the element will have many attributes with in the element(Main,Hdr etc.,)
    – subash Jul 31 '15 at 05:49
  • please note that the xml has to be a valid xml to XSLT – Saurav Jul 31 '15 at 05:53
  • Please don't post any code data in comment.Edit your question section – Saurav Jul 31 '15 at 06:52
  • I have placed the correct formatted xml file in the main post. Please suggest. – subash Jul 31 '15 at 07:03

1 Answers1

0

Hi there different ways how you can achieve what you want depending upon xslt version that you are using. I have used XSLT 1.0. From what you asked I have assumed the order of element arrangement and elements are pre-known to you.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:FIXML="fixprotocol.org/FIXML-4-4"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="FIXML msxsl">

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:apply-templates select="@* | node()"/>
  </xsl:template>

  <xsl:variable name="checkData">
    <data>Hdr</data>
    <data>Insert1</data>
    <data>Insert2</data>
  </xsl:variable>

  <xsl:template match="FIXML:Main">
    <Main>
      <xsl:variable name="temp" select="//*"/>
      <xsl:for-each select="msxsl:node-set($checkData)/*">
        <xsl:choose>
          <xsl:when test="count($temp[local-name(.)=current()]) &gt;= 1">
            <xsl:copy-of select="$temp[local-name(.)=current()]"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="{current()}"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Main>
  </xsl:template>

Variable "checkData" contains the element list you need to check already arranged. only other thing is that you have to use node-set function of your XSLT parser

Saurav
  • 592
  • 4
  • 21
  • Thank you. I tried the same XSLT file in OSB transformation. I got the below error. Error executing the XSLT transformation: java.lang.NoSuchMethodException: For extension function, could not find method weblogic.apache.xml.dtm.ref.DTMNodeIterator.node-set([ExpressionContext,] ). – subash Jul 31 '15 at 10:06
  • you have to find an equivalent node-set() function within your xslt parser – Saurav Jul 31 '15 at 10:37
  • if you can, use XSLT 2.0, it has support for node-set() (aka, you could iterate directly on variable nodes, no need to use a node-set() function). – Eric S Jul 31 '15 at 12:34
  • thanks Eric, OSB (Oracle Service Bus) supports XSLT 1.0 version only. – subash Jul 31 '15 at 13:11
  • Arf, sorry for this useless comment then :-s – Eric S Jul 31 '15 at 14:04
  • Any help on this with relate to OSB XSLT check. – subash Aug 05 '15 at 10:47