Groups > Design > Microsoft xsl > Re: xslt (identity?) transformation




xslt (identity?) transformation

xslt (identity?) transformation
Mon, 25 Feb 2008 06:50:17 -080
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
Post Reply
Re: xslt (identity?) transformation
Mon, 25 Feb 2008 17:07:42 +020
<?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
Post Reply
Re: xslt (identity?) transformation
Tue, 26 Feb 2008 03:10:11 -080
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 <m...@be.next> 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 -
Post Reply
Re: xslt (identity?) transformation
Tue, 26 Feb 2008 04:51:53 -080
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 <mahotr...@yahoo.de> 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/
Post Reply
Re: xslt (identity?) transformation
Tue, 26 Feb 2008 12:55:03 -080
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))]"/>&q
uot;
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 <mahotr...@yahoo.de> 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/
Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact