the input xml:
Code:
<ISA>
<GS>
<ST>
<SLN>
<SLN01>1</SLN01>
<SLN02>1</SLN02>
<SLN03>A</SLN03>
<SLN04>1</SLN04>
<SLN05>EA</SLN05>
</SLN>
<N9>
<N901>ME</N901>
<N902>STAT</N902>
</N9>
<MTX>
<MTX02>WORKING 20110803</MTX02>
</MTX>
<SLN>
<SLN01>1</SLN01>
<SLN02>2</SLN02>
<SLN03>A</SLN03>
<SLN04>1</SLN04>
<SLN05>EA</SLN05>
</SLN>
<N9>
<N901>82</N901>
<N902>EXKEY</N902>
<N903>603880</N903>
</N9>
<N9>
<N901>82</N901>
<N902>SWDESC</N902>
</N9>
<MTX>
<MTX02>5ESS</MTX02>
</MTX>
<CTT>
<CTT01>1</CTT01>
</CTT>
</ST>
</GS>
</ISA>
The xsl codes which I use:
Code:
<xsl:if test="N9[N901='82' and N902='SWDESC']/following::MTX/MTX02">
<xsl:element name="Detail">
<xsl:attribute name="tag">SWDESC</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="N9[N901='82' and N902='SWDESC']/following::MTX/MTX02"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
<xsl:if test="N9[N901='82' and N902='LN']/following::MTX/MTX02">
<xsl:element name="Detail">
<xsl:attribute name="tag">LN</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="N9[N901='82' and N902='LN']/following::MTX/MTX02"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
<xsl:if test="N9[N901='ME' and N902='STAT']/following::MTX/MTX02">
<xsl:element name="Detail">
<xsl:attribute name="tag">STAT</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="N9[N901='ME' and N902='STAT']/following::MTX/MTX02"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
Then, the java program which is use the xsl throws ArrayIndexOutOfBoundException. I found when the xsl code checks the fields in the xml, if the element is not there, like "LN", then the program throws exception, if the element there, it can work. I also found if I remove the "/folloing::MTX/MTX02" in the "if", then it can work, no matter the element in the xml or not. Why? And is there any alternative method to that?
(Note: the elements in the xml may in there or not, so I need the xsl to check whether these elements are there, if there, then output as Detail, if not there, then do nothing.)
Thanks.