I am using SaxonHE.
I need to check children nodes of Node N9, based on the children's values, then I need to get the child (children) of the node which is N9's sibling and just following N9.
My input XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<ISA>
<ISA01>00</ISA01>
<ISA02> </ISA02>
<ISA03>00</ISA03>
<GS>
<GS01>PR</GS01>
<GS02>FTRPO</GS02>
<ST>
<ST01>855</ST01>
<ST02>0001</ST02>
<BAK>
<BAK01>11</BAK01>
<BAK02>AT</BAK02>
<BAK03>117624</BAK03>
<BAK04>20020321</BAK04>
</BAK>
<DTM>
<DTM01>097</DTM01>
<DTM02>20020321</DTM02>
<DTM03>162329</DTM03>
</DTM>
<SI>
<SI01>TI</SI01>
<SI02>IQ</SI02>
<SI03>A</SI03>
</SI>
<N1>
<N101>BY</N101>
<N103>25</N103>
<N104>180A</N104>
</N1>
<PO1>
<PO101>1</PO101>
<PO102>1</PO102>
<PO103>EA</PO103>
<PO106>A6</PO106>
<PO107>ERR</PO107>
</PO1>
<N9>
<N901>1Q</N901>
<N902>FTRAVQ097</N902>
<N903>PRESPC</N903>
</N9>
<MTX>
<MTX02>WTN MAY ONLY BE 10 NUMERICS</MTX02>
</MTX>
<MTX>
<MTX02>Following another MTX02</MTX02>
</MTX>
<SI>
<SI01>TI</SI01>
<SI02>LO</SI02>
<SI03>FTWYINIZH02</SI03>
</SI>
<SI>
<SI01>TI</SI01>
<SI02>AF</SI02>
<SI03>N</SI03>
</SI>
<SI>
<SI01>TI</SI01>
<SI02>CF</SI02>
<SI03>0</SI03>
</SI>
<MTX>
<MTX02>Following the third MTX02</MTX02>
</MTX>
<CTT>
<CTT01>1</CTT01>
</CTT>
</ST>
</GS>
</ISA>
My XSLT codes:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
exclude-result-prefixes="xs">
<xsl:template match="ISA">
<AV>
<RES>
<xsl:if test="GS/ST/BAK[BAK01 = '11' and BAK02 = 'AT']/BAK03">
<xsl:attribute name="Number">
<xsl:value-of select="GS/ST/BAK[BAK01 = '11' and BAK02 = 'AT']/BAK03"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="GS/ST/DTM[DTM01 = '097']">
<xsl:attribute name="Time">
<xsl:value-of select="GS/ST/DTM[DTM01='097']/DTM02"/>
<xsl:value-of select="GS/ST/DTM[DTM01='097']/DTM03"/>
</xsl:attribute>
</xsl:if>
<xsl:choose>
<xsl:when test="GS/ST/N9[N901='1Q' and N903='PRESPC']/N902">
<xsl:attribute name="Code">
<xsl:value-of select="GS/ST/N9[N901='1Q' and N903='PRESPC']/N902"/>
</xsl:attribute>
<xsl:attribute name="Description">
<xsl:value-of select="following-sibling::GS/ST/N9[N901='1Q' and N903='PRESPC'][1]/MTX02"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="Code">000</xsl:attribute>
<xsl:attribute name="Description">Completed</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</RES>
<xsl:choose>
<xsl:when test="GS/ST/PO1[PO101='1' and PO102='1' and PO103='EA' and PO106='A6' and PO107='GOOD']">
<AD>Not for this case.</AD>
</xsl:when>
<xsl:otherwise>
<AD>
<xsl:element name="ERROR">
<xsl:if test="GS/ST/PO1[PO101='1' and PO102='1' and PO103='EA' and PO106='A6' and PO107='ERR']">
<xsl:attribute name="Code">
<xsl:value-of select="GS/ST/N9[N901='1Q' and N903='PRESPC']/N902"/>
</xsl:attribute>
<xsl:attribute name="Description">
<xsl:value-of select="following-sibling::GS/ST/N9[N901='1Q' and N903='PRESPC'][1]/MTX02"/>
</xsl:attribute>
</xsl:if>
</xsl:element>
</AD>
</xsl:otherwise>
</xsl:choose>
</AV>
</xsl:template>
</xsl:stylesheet>
My output XML should be:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<AV>
<RES Number="117624" Time="20020321162329" Code="FTRAVQ097" Description="Tele MAY ONLY BE 10 NUMERICS"/>
<AD>
<ERROR Code="FTRAVQ097" Description="Tele MAY ONLY BE 10 NUMERICS"/>
</AD>
</AV>
Right now, for my XSLT code, the output node's attribute Description is empty string (its value should be the first MTX/MTX02 following N9), and its value should be based on based on children's values of N9. I used the following-sibling, but it does not work.
Thank you in advance!