You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Alan Andrade <al...@insurity.com> on 2007/05/23 21:50:30 UTC

mege two separate xml nodes into one

How do I mege the 2 nodes?

INPUT:

 

<AllBooks>

 <BookList1>

   <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>

   <Book id="3" upc= "3" someotherID= "booklist1" salePrice= "3"/>

   <Book id="4" upc= "4" someotherID= "booklist1" salePrice= "4"/>

 </BookList1>

 <BookList2>

   <Book id="2" upc= "2" someotherID= "booklist2" salePrice= "2"/>

   <Book id= "3" upc= "3" someotherID= "booklist2" salePrice= "3"/>

   <Book id= "4" upc= "4" someotherID= "booklist2" salePrice= "4"/>

   <Book id= "5" upc= "5" someotherID= "booklist2" salePrice= "5"/>

 </BookList2>

</AllBooks>

 

OUTPUT:

 

<AllBooks>

 <BookList>

   <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>

   <Book id="2" upc= "3" someotherID= "booklist1" salePrice= "3"/>

   <Book id="3" upc= "4" someotherID= "booklist1" salePrice= "4"/>

   <Book id="4" upc= "2" someotherID= "booklist2" salePrice= "2"/>

   <Book id= "5" upc= "3" someotherID= "booklist2" salePrice= "3"/>

   <Book id= "6" upc= "4" someotherID= "booklist2" salePrice= "4"/>

   <Book id= "7" upc= "5" someotherID= "booklist2" salePrice= "5"/>

 </BookList>

</AllBooks>

 

Thanks

Alan

 

 

 



-----------------------------------------
The information contained in this e-mail message is intended only
for the personal and confidential use of the recipient(s) named
above. This message may be an attorney-client communication and/or
work product and as such is privileged and confidential. If the
reader of this message is not the intended recipient or an agent
responsible for delivering it to the intended recipient, you are
hereby notified that you have received this document in error and
that any review, dissemination, distribution, or copying of this
message is strictly prohibited. If you have received this
communication in error, please notify us immediately by e-mail, and
delete the original message.

Re: I need to remove format

Posted by David Bertoni <db...@apache.org>.
Alan Andrade wrote:
> Hi
> I have an input string as
> (123) 456-789
> And in the output I need
> 123456789
> 
> How do I do it in xslt.
> 
> I did
> <xsl:value-of select = "format-number(.,'#')" />
> The outut was
>   (125)4568956
> It doesn’t  get rid of the braces.
Since "(123) 456-789" isn't a valid lexical representation for a number, 
  I find it hard to believe you're getting that result with Xalan-J.

You might be better served by the "double-translate" idiom.  Here's a 
sample stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">

<xsl:output indent="yes" />

<xsl:template match="/">
   <out>
     <xsl:variable name="str" select="'(123) 456-789'" />
     <format-number>
       <xsl:value-of select="format-number($str,'#')" />
     </format-number>
     <double-translate>
       <xsl:value-of select="translate($str, translate($str, 
'123456789', ''), '')" />
     </double-translate>
   </out>
</xsl:template>

</xsl:stylesheet>

which produces:

<?xml version="1.0" encoding="UTF-8"?><out>
<format-number>NaN</format-number>
<double-translate>123456789</double-translate>
</out>

Dave


I need to remove format

Posted by Alan Andrade <al...@insurity.com>.
Hi

I have an input string as 

(123) 456-789

And in the output I need 

123456789

 

How do I do it in xslt.

 

I did 

<xsl:value-of select = "format-number(.,'#')" />

The outut was 

  (125)4568956

It doesn't  get rid of the braces.

 

Let me know

Thanks

Alan

 

 

 

 

________________________________

From: Alan Andrade [mailto:alan.andrade@insurity.com] 
Sent: Wednesday, May 23, 2007 12:50 PM
To: xalan-j-users@xml.apache.org
Subject: mege two separate xml nodes into one

 

How do I mege the 2 nodes?

INPUT:

 

<AllBooks>

 <BookList1>

   <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>

   <Book id="3" upc= "3" someotherID= "booklist1" salePrice= "3"/>

   <Book id="4" upc= "4" someotherID= "booklist1" salePrice= "4"/>

 </BookList1>

 <BookList2>

   <Book id="2" upc= "2" someotherID= "booklist2" salePrice= "2"/>

   <Book id= "3" upc= "3" someotherID= "booklist2" salePrice= "3"/>

   <Book id= "4" upc= "4" someotherID= "booklist2" salePrice= "4"/>

   <Book id= "5" upc= "5" someotherID= "booklist2" salePrice= "5"/>

 </BookList2>

</AllBooks>

 

OUTPUT:

 

<AllBooks>

 <BookList>

   <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>

   <Book id="2" upc= "3" someotherID= "booklist1" salePrice= "3"/>

   <Book id="3" upc= "4" someotherID= "booklist1" salePrice= "4"/>

   <Book id="4" upc= "2" someotherID= "booklist2" salePrice= "2"/>

   <Book id= "5" upc= "3" someotherID= "booklist2" salePrice= "3"/>

   <Book id= "6" upc= "4" someotherID= "booklist2" salePrice= "4"/>

   <Book id= "7" upc= "5" someotherID= "booklist2" salePrice= "5"/>

 </BookList>

</AllBooks>

 

Thanks

Alan

 

 

 

________________________________

The information contained in this e-mail message is intended only for
the personal and confidential use of the recipient(s) named above. This
message may be an attorney-client communication and/or work product and
as such is privileged and confidential. If the reader of this message is
not the intended recipient or an agent responsible for delivering it to
the intended recipient, you are hereby notified that you have received
this document in error and that any review, dissemination, distribution,
or copying of this message is strictly prohibited. If you have received
this communication in error, please notify us immediately by e-mail, and
delete the original message. 




-----------------------------------------
The information contained in this e-mail message is intended only
for the personal and confidential use of the recipient(s) named
above. This message may be an attorney-client communication and/or
work product and as such is privileged and confidential. If the
reader of this message is not the intended recipient or an agent
responsible for delivering it to the intended recipient, you are
hereby notified that you have received this document in error and
that any review, dissemination, distribution, or copying of this
message is strictly prohibited. If you have received this
communication in error, please notify us immediately by e-mail, and
delete the original message.

Re: mege two separate xml nodes into one

Posted by Matthijs Wensveen <m....@func.nl>.
Looking at the expected output, there seems to be some logic to 
implement. The place to do that is where Mukul put his 
copy-of-statement, or maybe even the Book element itself. My experience 
with 'merging' is that you really have to know what you're doing. What 
do you do when some attributes match, but others don't? What if the 
element text or children don't match? Usually this depends on business 
logic, and I'm not a big fan of implementing that in xslt. I'd put it 
somewhere where it can be unit tested (there's bound to be some unit 
tester for xslt, I guess... anyone?).

Also, be careful when your boss says: "oh you just have to merge the 
data", because the word 'merge' sounds as if there is no logic, but 
almost always there is. I've been there :)

Matthijs

Mukul Gandhi wrote:
> Following is a solution for this:
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> version="1.0">
>
>  <xsl:output method="xml" indent="yes" />
>
>  <xsl:template match="AllBooks">
>    <AllBooks>
>      <BookList>
>        <xsl:for-each select="*/*">
>          <Book id="{position()}">
>            <xsl:copy-of select="@*[not(name() = 'id')]" />
>          </Book>
>        </xsl:for-each>
>      </BookList>
>    </AllBooks>
>  </xsl:template>
>
> </xsl:stylesheet>
>
> On 5/24/07, Alan Andrade <al...@insurity.com> wrote:
>>
>>
>>
>> How do I mege the 2 nodes?
>>
>> INPUT:
>>
>>
>>
>> <AllBooks>
>>
>>  <BookList1>
>>
>>    <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>
>>
>>    <Book id="3" upc= "3" someotherID= "booklist1" salePrice= "3"/>
>>
>>    <Book id="4" upc= "4" someotherID= "booklist1" salePrice= "4"/>
>>
>>  </BookList1>
>>
>>  <BookList2>
>>
>>    <Book id="2" upc= "2" someotherID= "booklist2" salePrice= "2"/>
>>
>>    <Book id= "3" upc= "3" someotherID= "booklist2" salePrice= "3"/>
>>
>>    <Book id= "4" upc= "4" someotherID= "booklist2" salePrice= "4"/>
>>
>>    <Book id= "5" upc= "5" someotherID= "booklist2" salePrice= "5"/>
>>
>>  </BookList2>
>>
>> </AllBooks>
>>
>>
>>
>> OUTPUT:
>>
>>
>>
>> <AllBooks>
>>
>>  <BookList>
>>
>>    <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>
>>
>>    <Book id="2" upc= "3" someotherID= "booklist1" salePrice= "3"/>
>>
>>    <Book id="3" upc= "4" someotherID= "booklist1" salePrice= "4"/>
>>
>>    <Book id="4" upc= "2" someotherID= "booklist2" salePrice= "2"/>
>>
>>    <Book id= "5" upc= "3" someotherID= "booklist2" salePrice= "3"/>
>>
>>    <Book id= "6" upc= "4" someotherID= "booklist2" salePrice= "4"/>
>>
>>    <Book id= "7" upc= "5" someotherID= "booklist2" salePrice= "5"/>
>>
>>  </BookList>
>>
>> </AllBooks>
>>
>>
>>
>> Thanks
>>
>> Alan
>>
>>
>>
>>
>>
>>
>>
>>
>> ________________________________
>>
>>
>>
>>
>> The information contained in this e-mail message is intended only for 
>> the
>> personal and confidential use of the recipient(s) named above. This 
>> message
>> may be an attorney-client communication and/or work product and as 
>> such is
>> privileged and confidential. If the reader of this message is not the
>> intended recipient or an agent responsible for delivering it to the 
>> intended
>> recipient, you are hereby notified that you have received this 
>> document in
>> error and that any review, dissemination, distribution, or copying of 
>> this
>> message is strictly prohibited. If you have received this 
>> communication in
>> error, please notify us immediately by e-mail, and delete the original
>> message.
>
>


-- 
Matthijs Wensveen
Func. Internet Integration
W http://www.func.nl
T +31 20 4230000
F +31 20 4223500 


Re: mege two separate xml nodes into one

Posted by Mukul Gandhi <ga...@gmail.com>.
Following is a solution for this:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="AllBooks">
    <AllBooks>
      <BookList>
        <xsl:for-each select="*/*">
          <Book id="{position()}">
            <xsl:copy-of select="@*[not(name() = 'id')]" />
          </Book>
        </xsl:for-each>
      </BookList>
    </AllBooks>
  </xsl:template>

</xsl:stylesheet>

On 5/24/07, Alan Andrade <al...@insurity.com> wrote:
>
>
>
> How do I mege the 2 nodes?
>
> INPUT:
>
>
>
> <AllBooks>
>
>  <BookList1>
>
>    <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>
>
>    <Book id="3" upc= "3" someotherID= "booklist1" salePrice= "3"/>
>
>    <Book id="4" upc= "4" someotherID= "booklist1" salePrice= "4"/>
>
>  </BookList1>
>
>  <BookList2>
>
>    <Book id="2" upc= "2" someotherID= "booklist2" salePrice= "2"/>
>
>    <Book id= "3" upc= "3" someotherID= "booklist2" salePrice= "3"/>
>
>    <Book id= "4" upc= "4" someotherID= "booklist2" salePrice= "4"/>
>
>    <Book id= "5" upc= "5" someotherID= "booklist2" salePrice= "5"/>
>
>  </BookList2>
>
> </AllBooks>
>
>
>
> OUTPUT:
>
>
>
> <AllBooks>
>
>  <BookList>
>
>    <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/>
>
>    <Book id="2" upc= "3" someotherID= "booklist1" salePrice= "3"/>
>
>    <Book id="3" upc= "4" someotherID= "booklist1" salePrice= "4"/>
>
>    <Book id="4" upc= "2" someotherID= "booklist2" salePrice= "2"/>
>
>    <Book id= "5" upc= "3" someotherID= "booklist2" salePrice= "3"/>
>
>    <Book id= "6" upc= "4" someotherID= "booklist2" salePrice= "4"/>
>
>    <Book id= "7" upc= "5" someotherID= "booklist2" salePrice= "5"/>
>
>  </BookList>
>
> </AllBooks>
>
>
>
> Thanks
>
> Alan
>
>
>
>
>
>
>
>
> ________________________________
>
>
>
>
> The information contained in this e-mail message is intended only for the
> personal and confidential use of the recipient(s) named above. This message
> may be an attorney-client communication and/or work product and as such is
> privileged and confidential. If the reader of this message is not the
> intended recipient or an agent responsible for delivering it to the intended
> recipient, you are hereby notified that you have received this document in
> error and that any review, dissemination, distribution, or copying of this
> message is strictly prohibited. If you have received this communication in
> error, please notify us immediately by e-mail, and delete the original
> message.


-- 
Regards,
Mukul Gandhi

Re: merge or delete duplicates

Posted by Mukul Gandhi <ga...@gmail.com>.
I think this is a grouping problem, and we can use Muenchian grouping
to solve it.

Below is the stylesheet to solve this ...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	       version="1.0">

  <xsl:output method="text" />

  <xsl:variable name="caps" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
  <xsl:variable name="small" select="'abcdefghijklmnopqrstuvwxyz'" />

  <xsl:key name="by-name" match="UnderWriterDataInput/*"
use="translate(local-name(), $small, $caps)" />

  <xsl:template match="UnderWriterDataInput">
    <xsl:for-each select="*[generate-id() = generate-id(key('by-name',
translate(local-name(), $small, $caps))[1])]">
      <xsl:value-of select="translate(local-name(), $small, $caps)" />
: <xsl:value-of select="." />
      <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>


On 8/12/08, Alan Andrade <al...@insurity.com> wrote:
>
>
>
> Input:
>
> < UnderWriterDataInput ><q20004>NO</q20004>
>
> <Q20006>NO</Q20006>
>
> <Q20006>NO</Q20006>
>
> <Q20004>NO</Q20004>
>
> </ UnderWriterDataInput >
>
>
>
> OutPut:
>
>
>
> Q20006 NO
>
> Q20004 NO
>
>
>
> XSLT:
>
> <xsl:variable name="unique-list"
> select="//UnderWriterDataInput/child::*[not(name()=following::name)]"
> />
>
>     <xsl:for-each select="$unique-list">
>
>         <xsl:value-of select="name()" />
>
>         <xsl:value-of select="." />
>
>       </xsl:for-each>
>
>
>
> His does not work!! Let me know what is needed to be corrected.
>
> Thanks
>
> Alan


-- 
Regards,
Mukul Gandhi

Re: merge or delete duplicates

Posted by ke...@us.ibm.com.
> child::*[not(name()=following::name)]"

following::name means all of the the following nodes which are <name> 
elements. (And not even limited to following sibling).

You probably meant something more like
name(following-sibling::*[1])

--- which checks the name of the next element sibling.



There may be other errors; this is just the first one that jumped out at 
me.


Reminder: the XSLT FAQ website has a lot of good examples. Worth studying.

merge or delete duplicates

Posted by Alan Andrade <al...@insurity.com>.
Input:

< UnderWriterDataInput ><q20004>NO</q20004>

<Q20006>NO</Q20006>

<Q20006>NO</Q20006>

<Q20004>NO</Q20004>

</ UnderWriterDataInput >

 

OutPut:

 

Q20006 NO 

Q20004 NO

 

XSLT:

<xsl:variable name="unique-list"
select="//UnderWriterDataInput/child::*[not(name()=following::name)]" />

    <xsl:for-each select="$unique-list">

        <xsl:value-of select="name()" />

        <xsl:value-of select="." />

      </xsl:for-each>

 

His does not work!! Let me know what is needed to be corrected.

Thanks

Alan

 

________________________________

The information contained in this e-mail message is intended only for
the personal and confidential use of the recipient(s) named above. This
message may be an attorney-client communication and/or work product and
as such is privileged and confidential. If the reader of this message is
not the intended recipient or an agent responsible for delivering it to
the intended recipient, you are hereby notified that you have received
this document in error and that any review, dissemination, distribution,
or copying of this message is strictly prohibited. If you have received
this communication in error, please notify us immediately by e-mail, and
delete the original message. 




-----------------------------------------
The information contained in this e-mail message is intended only
for the personal and confidential use of the recipient(s) named
above. This message may be an attorney-client communication and/or
work product and as such is privileged and confidential. If the
reader of this message is not the intended recipient or an agent
responsible for delivering it to the intended recipient, you are
hereby notified that you have received this document in error and
that any review, dissemination, distribution, or copying of this
message is strictly prohibited. If you have received this
communication in error, please notify us immediately by e-mail, and
delete the original message.