Subject: Re: [xsl] Selecting a particular node of an XML generated from Excel From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx> Date: Wed, 9 Nov 2011 12:12:37 +0100 |
I reduced the row in your sample from 117 to 7 and added the missing namespace. Also I added an "X" to the Data content for the rows you are interested to see. This allows for easier result verification. $ xsltproc karl.xsl karl.xml <Cell xmlns:ss="foo"><Data>YES X</Data></Cell> <Cell xmlns:ss="foo"><Data>YES X</Data></Cell> <Cell xmlns:ss="foo" ss:Index="7"><Data>NO X</Data></Cell> $ $ cat karl.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ss="foo" > <xsl:output omit-xml-declaration="yes"/> <xsl:template match="/root/Row"> <xsl:variable name="row" select="7"/> <xsl:for-each select="Cell"> <xsl:variable name="last-index" select="preceding-sibling::Cell[@ss:Index][1]"/> <xsl:if test="(@ss:Index = $row) or (not($last-index) and (position() = $row)) or ($last-index and (count (preceding-sibling::Cell [$last-index = preceding-sibling::Cell] ) + $last-index/@ss:Index + 1 = $row ) )" > <xsl:copy-of select="."/> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet> $ $ cat karl.xml <root xmlns:ss="foo"> <Row> <Cell><Data>Hydrogen and helium nuclei form in the first three minutes...</Data></Cell> <Cell ss:Index="2"><Data>107</Data></Cell> <Cell><Data>109</Data></Cell> <Cell ss:Index="5"><Data>115</Data></Cell> <Cell><Data>116</Data></Cell> <Cell><Data>YES X</Data></Cell> <!-- this is 7th row. But position () != 7 --> <Cell><Data>118</Data></Cell> </Row> <Row> <Cell><Data>u2</Data></Cell> <Cell><Data>2 million yearsgo</Data></Cell> <Cell><Data>blahlah</Data></Cell> <Cell><Data>1</Data></Cell> <Cell><Data>2</Data></Cell> <Cell><Data>6</Data></Cell> <!-- SO MANY ROWS HERE --> <Cell><Data>YES X</Data></Cell> <!-- THIS IS 7th row (Cell) i.e. position() = 7 --> <!--GAIN SO MANY ROWS HERE --> <Cell><Data>199</Data></Cell> <Cell><Data>200</Data></Cell> <!--ASSUME THIS IS 200TH ROW --> </Row> <Row> <Cell><Data>u3</Data></Cell> <Cell><Data>20th century</Data></Cell> <Cell><Data>blah blah</Data></Cell> <Cell ss:Index="7"><Data>NO X</Data></Cell> <!-- THIS IS 7th row But position() != 7 --> <Cell ss:Index="199"><Data>119</Data></Cell> </Row> </root> $ Mit besten Gruessen / Best wishes, Hermann Stamm-Wilbrandt Level 3 support for XML Compiler team, Fixpack team lead WebSphere DataPower SOA Appliances https://www.ibm.com/developerworks/mydeveloperworks/blogs/HermannSW/ ---------------------------------------------------------------------- IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Martin Jetter Geschaeftsfuehrung: Dirk Wittkopp Sitz der Gesellschaft: Boeblingen Registergericht: Amtsgericht Stuttgart, HRB 243294 From: Karlmarx R <karlmarxr@xxxxxxxxx> To: xsl-list Mulberry <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>, Date: 11/08/2011 09:49 PM Subject: [xsl] Selecting a particular node of an XML generated from Excel Hello, I have a requirement where I am not sure of the best possible solution. The XML is generated from Excel. The problem is with missing rows which makes it difficult to select a particular node. Better I explain using this sample XML: [Pls ignore any case mismatch or spell mistakes] <root> <Row> <Cell><Data>u1</Data></Cell> <Cell><Data>13.7 billion years ago</Data></Cell> <Cell><Data>Hydrogen and helium nuclei form in the first three minutes...</Data></Cell> <Cell ss:Index="107"><Data>107</Data></Cell> <Cell><Data>108</Data></Cell> <Cell><Data>109</Data></Cell> <Cell ss:Index="115"><Data>115</Data></Cell> <Cell><Data>116</Data></Cell> <Cell><Data>YES</Data></Cell> <!-- this is 117th row. But position () != 117 --> <Cell><Data>118</Data></Cell> </Row> <Row> <Cell><Data>u2</Data></Cell> <Cell><Data>2 million yearsgo</Data></Cell> <Cell><Data>blahlah</Data></Cell> <Cell><Data>1</Data></Cell> <Cell><Data>2</Data></Cell> <!-- SO MANY ROWS HERE --> <Cell><Data>YES</Data></Cell> <!-- THIS IS 117th row (Cell) i.e. position() = 117 --> <!--GAIN SO MANY ROWS HERE --> <Cell><Data>199</Data></Cell> <Cell><Data>200</Data></Cell> <!--ASSUME THIS IS 200TH ROW --> </Row> <Row> <Cell><Data>u3</Data></Cell> <Cell><Data>20th century</Data></Cell> <Cell><Data>blah blah</Data></Cell> <Cell ss:Index="117"><Data>NO</Data></Cell> <!-- THIS IS 117th row But position() != 117 --> <Cell ss:Index="199"><Data>119</Data></Cell> </Row> </root> The excel to xml convertion results in empty excel columns getting stripped out in the resulting <Cell>. And so, the number of <Cell>'s vary for each <Row>. Now, if I want to select a particular <Cell>, say 117th <Cell>, I need to check any @ss:Index exist and if so, count() the number of the preceding-sibling's + the preceding @ss:Index value. So, for 117th Cell value, (A) in Row 1, 2nd <Cell> of @ss:Indiex is 117th (B) in Row 2, position() = 117 is the required one (C) in Row 3, @ss:Indiex matching 117 (D) if none matching found, ignore In my requirement, I need to select the N-th <Cell> value and check whether it is 'yes' or 'no' and then process. I am trying approach something like this (incomplete) <xsl:for-each select="Cell"> ... <xsl:variable name="last-index " select="preceding-sibling::Cell [@ss:Index][1]"/> <!-- the ABOVE giver the last <Cell>'s ss:Index value. Now I need to do something like count(preceding-sibling::Cell[@ss:Index][1]) - this may be wrong, but here is what I having trouble and need suggestions and best approach to solve this, to select N-th <Cell>, where N is known and can be 1 to X (=max possible value). Thanks in advance!. Regards, karl --~------------------------------------------------------------------ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/ or e-mail: < mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx> --~--
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Selecting a particular no, Michael Kay | Thread | Re: [xsl] Selecting a particular no, leegold |
Re: [xsl] Can group-by treat its ta, Jirka Kosek | Date | Re: [xsl] Selecting a particular no, leegold |
Month |