Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Mon, 25 Feb 2008 06:50:17 -0800 (PST),    group: microsoft.public.xsl        back       


xslt (identity?) transformation   
I can't seem to get my head around this problem, although from the
examples I've seen from googling identity transform etc. I seems it
should be pretty simple - but I'm stuck :-s:

How do I transform xml_file_1 to xml_file_2?

---- xml_file_1--------
<books>
   <author lastname="lastname_1" givenname="given_1">
      <book title="title_1">
      <book title="title_2">
      <book title="title_3">
   <author lastname="lastname_2" givenname="given_2">
      <book title="title_21">
      <book title="title_22">
      <book title="title_23">
   <author lastname="lastname_3" givenname="given_3">
      <book title="title_31">
      <book title="title_32">
      <book title="title_33">
</books>
---- xml_file_1--------

during the transformation I want to "strip out" all other (hundreds)
and end up with a few given authors that are identified by let's say
their "lastname" attribute.
authors is always located at the same level in the structure.

This is the most important task, and should end up in this file:

---- xml_file_2--------
<books>
   <author lastname="lastname_2" givenname="given_2">
      <book title="title_21">
      <book title="title_22">
      <book title="title_23">
</books>
---- xml_file_2 --------

Later I'd like to be able to process this file again to a third file -
still same structure, but be able to strip certain books out
identified by the "title" attribute, and in my real world example
these books might be several levels down in the structure...
date: Mon, 25 Feb 2008 06:50:17 -0800 (PST)   author:   unknown

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

     <xsl:template match="@* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
     </xsl:template>

   <xsl:template match="author[@lastname != 'lastname_2']"/>

</xsl:stylesheet>

or (with positive logic):

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

     <xsl:template match="@* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
     </xsl:template>

   <xsl:template match="author"/>

   <xsl:template match="author[@lastname = 'lastname_2']">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>


-- 
Oleg

roger@langedal.com wrote:
> I can't seem to get my head around this problem, although from the
> examples I've seen from googling identity transform etc. I seems it
> should be pretty simple - but I'm stuck :-s:
> 
> How do I transform xml_file_1 to xml_file_2?
> 
> ---- xml_file_1--------
> <books>
>    <author lastname="lastname_1" givenname="given_1">
>       <book title="title_1">
>       <book title="title_2">
>       <book title="title_3">
>    <author lastname="lastname_2" givenname="given_2">
>       <book title="title_21">
>       <book title="title_22">
>       <book title="title_23">
>    <author lastname="lastname_3" givenname="given_3">
>       <book title="title_31">
>       <book title="title_32">
>       <book title="title_33">
> </books>
> ---- xml_file_1--------
> 
> during the transformation I want to "strip out" all other (hundreds)
> and end up with a few given authors that are identified by let's say
> their "lastname" attribute.
> authors is always located at the same level in the structure.
> 
> This is the most important task, and should end up in this file:
> 
> ---- xml_file_2--------
> <books>
>    <author lastname="lastname_2" givenname="given_2">
>       <book title="title_21">
>       <book title="title_22">
>       <book title="title_23">
> </books>
> ---- xml_file_2 --------
> 
> Later I'd like to be able to process this file again to a third file -
> still same structure, but be able to strip certain books out
> identified by the "title" attribute, and in my real world example
> these books might be several levels down in the structure...
date: Mon, 25 Feb 2008 17:07:42 +0200   author:   Oleg Tkachenko

Re: xslt (identity?) transformation   
Thank you Oleg :-)

I really do appreciate your excellent feedback - seems simple enought
- will start reading myself up on XSL as soon as time permitts :-)

Your code picks the right toplevel nodes - but no childnodes are
copied....?

and if you have the time - is there any way to exclude childnodes(even
multiple levels down) of "author" based on an attribute-value as well?



On 25 Feb, 16:07, Oleg Tkachenko  wrote:
> <?xml version="1.0" encoding="utf-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
>      <xsl:template match="@* | node()">
>          <xsl:copy>
>              <xsl:apply-templates select="@* | node()"/>
>          </xsl:copy>
>      </xsl:template>
>
>    <xsl:template match="author[@lastname != 'lastname_2']"/>
>
> </xsl:stylesheet>
>
> or (with positive logic):
>
> <?xml version="1.0" encoding="utf-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
>      <xsl:template match="@* | node()">
>          <xsl:copy>
>              <xsl:apply-templates select="@* | node()"/>
>          </xsl:copy>
>      </xsl:template>
>
>    <xsl:template match="author"/>
>
>    <xsl:template match="author[@lastname = 'lastname_2']">
>      <xsl:copy>
>        <xsl:apply-templates select="@* | node()"/>
>      </xsl:copy>
>    </xsl:template>
>
> </xsl:stylesheet>
>
> --
> Oleg
>
>
>
> ro...@langedal.com wrote:
> > I can't seem to get my head around this problem, although from the
> > examples I've seen from googling identity transform etc. I seems it
> > should be pretty simple - but I'm stuck :-s:
>
> > How do I transform xml_file_1 to xml_file_2?
>
> > ---- xml_file_1--------
> > <books>
> >    <author lastname="lastname_1" givenname="given_1">
> >       <book title="title_1">
> >       <book title="title_2">
> >       <book title="title_3">
> >    <author lastname="lastname_2" givenname="given_2">
> >       <book title="title_21">
> >       <book title="title_22">
> >       <book title="title_23">
> >    <author lastname="lastname_3" givenname="given_3">
> >       <book title="title_31">
> >       <book title="title_32">
> >       <book title="title_33">
> > </books>
> > ---- xml_file_1--------
>
> > during the transformation I want to "strip out" all other (hundreds)
> > and end up with a few given authors that are identified by let's say
> > their "lastname" attribute.
> > authors is always located at the same level in the structure.
>
> > This is the most important task, and should end up in this file:
>
> > ---- xml_file_2--------
> > <books>
> >    <author lastname="lastname_2" givenname="given_2">
> >       <book title="title_21">
> >       <book title="title_22">
> >       <book title="title_23">
> > </books>
> > ---- xml_file_2 --------
>
> > Later I'd like to be able to process this file again to a third file -
> > still same structure, but be able to strip certain books out
> > identified by the "title" attribute, and in my real world example
> > these books might be several levels down in the structure...- Skjul sitert tekst -
>
> - Vis sitert tekst -
date: Tue, 26 Feb 2008 03:10:11 -0800 (PST)   author:   roger

Re: xslt (identity?) transformation   
roger wrote:

> Your code picks the right toplevel nodes - but no childnodes are
> copied....?

With the following XML

<books>
    <author lastname="lastname_1" givenname="given_1">
       <book title="title_1"/>
       <book title="title_2"/>
       <book title="title_3"/>
    </author>
    <author lastname="lastname_2" givenname="given_2">
       <book title="title_21"/>
       <book title="title_22"/>
       <book title="title_23"/>
    </author>
    <author lastname="lastname_3" givenname="given_3">
       <book title="title_31"/>
       <book title="title_32"/>
       <book title="title_33"/>
     </author>
</books>

and the following stylesheet

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

     <xsl:template match="@* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
     </xsl:template>

   <xsl:template match="author[@lastname != 'lastname_2']"/>

</xsl:stylesheet>

the result is

<books>

    <author lastname="lastname_2" givenname="given_2">
       <book title="title_21"/>
       <book title="title_22"/>
       <book title="title_23"/>
    </author>

</books>

Is that not what you want? It looks like the result you described in 
your initial post, at least when trying to make your samples well-formed 
XML.

> and if you have the time - is there any way to exclude childnodes(even
> multiple levels down) of "author" based on an attribute-value as well?

Please post well-formed samples of the XML input and the desired result.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Tue, 26 Feb 2008 13:10:21 +0100   author:   Martin Honnen

Re: xslt (identity?) transformation   
Oleg & Martin

I'm sorry guys - a fault on my part obviously, the reason the
childnodes was not copied was as Martin pointed out a result of me
messing up the sourceXML tags :-o

So basically: The code provided by Oleg worked flawlessly - the error
was on my part :-)

as for my second question:

if it possible to select all childelements of an "author" element -
EXCLUDING a few - based on the "title" attribute of the "book" element
in this example?

Thanks for your invaluable input on this so far :-)

Best regards,
Roger








On 26 Feb, 13:10, Martin Honnen  wrote:
> roger wrote:
> > Your code picks the right toplevel nodes - but no childnodes are
> > copied....?
>
> With the following XML
>
> <books>
>     <author lastname="lastname_1" givenname="given_1">
>        <book title="title_1"/>
>        <book title="title_2"/>
>        <book title="title_3"/>
>     </author>
>     <author lastname="lastname_2" givenname="given_2">
>        <book title="title_21"/>
>        <book title="title_22"/>
>        <book title="title_23"/>
>     </author>
>     <author lastname="lastname_3" givenname="given_3">
>        <book title="title_31"/>
>        <book title="title_32"/>
>        <book title="title_33"/>
>      </author>
> </books>
>
> and the following stylesheet
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
>      <xsl:template match="@* | node()">
>          <xsl:copy>
>              <xsl:apply-templates select="@* | node()"/>
>          </xsl:copy>
>      </xsl:template>
>
>    <xsl:template match="author[@lastname != 'lastname_2']"/>
>
> </xsl:stylesheet>
>
> the result is
>
> <books>
>
>     <author lastname="lastname_2" givenname="given_2">
>        <book title="title_21"/>
>        <book title="title_22"/>
>        <book title="title_23"/>
>     </author>
>
> </books>
>
> Is that not what you want? It looks like the result you described in
> your initial post, at least when trying to make your samples well-formed
> XML.
>
> > and if you have the time - is there any way to exclude childnodes(even
> > multiple levels down) of "author" based on an attribute-value as well?
>
> Please post well-formed samples of the XML input and the desired result.
>
> --
>
>         Martin Honnen --- MVP XML
>        http://JavaScript.FAQTs.com/
date: Tue, 26 Feb 2008 04:51:53 -0800 (PST)   author:   roger

Re: xslt (identity?) transformation   
roger wrote:

> if it possible to select all childelements of an "author" element -
> EXCLUDING a few - based on the "title" attribute of the "book" element
> in this example?

Well you could write a template for author elements that processes only 
some book child elements e.g.
   <xsl:template match="author">
     <xsl:copy>
       <xsl:apply-templates select="@*"/>
       <xsl:apply-templates select="book[contains(@title,'foo')]"/>
     </xsl:copy>
   </xsl:template>

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Tue, 26 Feb 2008 14:24:17 +0100   author:   Martin Honnen

Re: xslt (identity?) transformation   
Thank you Martin!

Putting it all together:

------ snippet ---------
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="author"/>

  <xsl:template match="author[@name = 'whatever_name_of_author]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/> <-- I want the attributes
      <xsl:apply-templates
select="book[NOT(contains(@title,'do_not_include_this_title))]"/>
    </xsl:copy>
  </xsl:template>
----- snippet ------

As I wanted to exclude the title I was searching for I've added the
not clause.

But one last thing - forgive me the stupid example with the books xml,
but my real world example is a bit more complex.
But for the sake of simplifying and sticking to the example:
What if I the books where nested and I wanted the "<xsl:apply-
templates
select="book[NOT(contains(@title,'do_not_include_this_title))]"/>"
to apply to all nested elements - no matter how deep they were
appearing under an author i still want to check the "title" attribute,
and if I find a book I do not like, discard it?

So - how do I shred away the <book title="title_212"/> from the XML
below when I do not know where in the structure it is , only that it
is below an author by a given name?

     <author lastname="lastname_2" givenname="given_2">
       <book title="title_21"/>
       <book title="title_22"/>
           <book title="title_211"/>
                <book title="title_212"/>
       <book title="title_23"/>
    </author>

And again - thank you for taking the time to show me how this is done
- I've already learned a lot, and will keep digging :-)







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

     <xsl:template match="@* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
     </xsl:template>

   <xsl:template match="author[@lastname != 'lastname_2']"/>


</xsl:stylesheet>




On 26 Feb, 14:24, Martin Honnen  wrote:
> roger wrote:
> > if it possible to select all childelements of an "author" element -
> > EXCLUDING a few - based on the "title" attribute of the "book" element
> > in this example?
>
> Well you could write a template for author elements that processes only
> some book child elements e.g.
>    <xsl:template match="author">
>      <xsl:copy>
>        <xsl:apply-templates select="@*"/>
>        <xsl:apply-templates select="book[contains(@title,'foo')]>      </xsl:copy>
>    </xsl:template>
>
> --
>
>         Martin Honnen --- MVP XML
>        http://JavaScript.FAQTs.com/
date: Tue, 26 Feb 2008 12:55:03 -0800 (PST)   author:   roger

Re: xslt (identity?) transformation   
Never mind guys - I found a way to do it :-) Thanks again to both of
you for invaluable help! :-)

Best regards,
Roger


On 26 Feb, 21:55, roger  wrote:
> Thank you Martin!
>
> Putting it all together:
>
> ------ snippet ---------
>   <xsl:template match="@* | node()">
>     <xsl:copy>
>       <xsl:apply-templates select="@* | node()"/>
>     </xsl:copy>
>   </xsl:template>
>
>   <xsl:template match="author"/>
>
>   <xsl:template match="author[@name = 'whatever_name_of_author]">
>     <xsl:copy>
>       <xsl:apply-templates select="@*"/> <-- I want the attributes
>       <xsl:apply-templates
> select="book[NOT(contains(@title,'do_not_include_this_title))]"/>
>     </xsl:copy>
>   </xsl:template>
> ----- snippet ------
>
> As I wanted to exclude the title I was searching for I've added the
> not clause.
>
> But one last thing - forgive me the stupid example with the books xml,
> but my real world example is a bit more complex.
> But for the sake of simplifying and sticking to the example:
> What if I the books where nested and I wanted the "<xsl:apply-
> templates
> select="book[NOT(contains(@title,'do_not_include_this_title))]"/>"
> to apply to all nested elements - no matter how deep they were
> appearing under an author i still want to check the "title" attribute,
> and if I find a book I do not like, discard it?
>
> So - how do I shred away the <book title="title_212"/> from the XML
> below when I do not know where in the structure it is , only that it
> is below an author by a given name?
>
>      <author lastname="lastname_2" givenname="given_2">
>        <book title="title_21"/>
>        <book title="title_22"/>
>            <book title="title_211"/>
>                 <book title="title_212"/>
>        <book title="title_23"/>
>     </author>
>
> And again - thank you for taking the time to show me how this is done
> - I've already learned a lot, and will keep digging :-)
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
>      <xsl:template match="@* | node()">
>          <xsl:copy>
>              <xsl:apply-templates select="@* | node()"/>
>          </xsl:copy>
>      </xsl:template>
>
>    <xsl:template match="author[@lastname != 'lastname_2']"/>
>
> </xsl:stylesheet>
>
> On 26 Feb, 14:24, Martin Honnen  wrote:
>
>
>
> > roger wrote:
> > > if it possible to select all childelements of an "author" element -
> > > EXCLUDING a few - based on the "title" attribute of the "book" element
> > > in this example?
>
> > Well you could write a template for author elements that processes only
> > some book child elements e.g.
> >    <xsl:template match="author">
> >      <xsl:copy>
> >        <xsl:apply-templates select="@*"/>
> >        <xsl:apply-templates select="book[contains(@title,'foo')]"/>
> >      </xsl:copy>
> >    </xsl:template>
>
> > --
>
> >         Martin Honnen --- MVP XML
> >        http://JavaScript.FAQTs.com/- Skjul sitert tekst -
>
> - Vis sitert tekst -
date: Wed, 27 Feb 2008 01:50:35 -0800 (PST)   author:   roger

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us