Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 20th, 2012, 11:09 AM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Question How to get the following node children based on the node condition

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!
 
Old April 20th, 2012, 11:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You are using following-sibling::GS at a point where the context node is an ISA element, but the ISA element does not have a following-sibling named GS. I'm not sure whose following-sibling you are looking for.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
JohnKiller (April 20th, 2012)
 
Old April 20th, 2012, 11:24 AM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Thank you so much for replying. I am looking for ISA/GS/ST/N9's sibling's child, but I need to check N9's children to make sure this N9 is the one I am looking for. So check N9's children, then get the N9's sibling's children.
 
Old April 20th, 2012, 11:51 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think instead of
Code:
following-sibling::GS/ST/N9[N901='1Q' and N903='PRESPC'][1]/MTX02
you want
Code:
GS/ST/N9[N901='1Q' and N903='PRESPC'][1]/following-sibling::MTX/MTX02
or perhaps only the first sibling e.g.
Code:
GS/ST/N9[N901='1Q' and N903='PRESPC'][1]/following-sibling::MTX[1]/MTX02
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
JohnKiller (April 20th, 2012)
 
Old April 20th, 2012, 12:08 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Thank you so much, Martin. I am confusing about the follow-sibling::node.
For my case,
Code:
GS/ST/N9[N901='1Q' and N903='PRESPC'][1]/following-sibling::MTX[1]/MTX02
I was thinking is should be ::N9, I would like to know why?
 
Old April 20th, 2012, 01:05 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well with XPath if you have a path expression selecting a node or a node-set or (XPath 2.0) a sequence of nodes then if you want to select nodes relative to those nodes or if you want to navigate to other nodes relative to those nodes then you add a step at the end of the expression.

And then you should make yourself familiar with the XPath axes with a book or tutorial like http://www.xmlplease.com/axis.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 20th, 2012, 01:10 PM
Authorized User
 
Join Date: Jul 2010
Posts: 74
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Thank you so much for the information, Martin. Recently we changed to use XSLT2.0 processor, and I do not get used of it. And the tutorial which you give is pretty good.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
Copy parent node and not its children bonekrusher XSLT 4 August 29th, 2007 08:44 AM
Multi Node Condition just2click XSLT 4 February 4th, 2007 04:55 PM
Counting node-set children in template rufustfirefly XSLT 2 May 3rd, 2004 08:48 AM
Trying to get node name of childs children keldan XML 1 August 1st, 2003 03:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.