Basic xslt transformation -


i have 1 xml request need modify (to xml) , send further. have no prior knowledge of xslt. have

<combined> <profile>   <fullname>john doe</fullname>   <otherdata>     <birthdate>1996</birthdate>     <favoritebooks>       <book>         <id>1</id>         <description>libre1</description>       </book>       <book>         <id>2</id>         <description>libre2</description>       </book>       <book>         <id>3</id>         <description></description>       </book>       <book>         <id>4</id>         <description>libre4</description>       </book>     </favoritebooks>   </otherdata> </profile> <loadeddata>   <newbirthdate>1998</newbirthdate>   <booksupdate>     <book id="1">       <booktext>book1</booktext>     </book>     <book id="2">       <booktext>book2</booktext>     </book>     <book id="3">       <booktext>book3</booktext>     </book>     <book id="4">       <booktext>book4</booktext>     </book>     <book id="5">       <booktext>book5</booktext>     </book>   </booksupdate> </loadeddata> 

and want get

<profile> <fullname>john doe</fullname> <otherdata>   <birthdate>1998</birthdate>   <favoritebooks>     <book>       <id>1</id>       <description>libre1book1</description>     </book>     <book>       <id>2</id>       <description>libre2book2</description>     </book>     <book>       <id>3</id>       <description>empty</description>     </book>     <book>       <id>4</id>       <description>libre4book4</description>     </book>     <book>       <id>5</id>       <description>new book5</description>     </book>   </favoritebooks> </otherdata> 

i did pretty pathetic attempt, not work.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"   xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" > <xsl:output method="xml" indent="yes"/> <xsl:template match="/">   <profile>     <xsl:apply-templates select="combined/profile/fullname" />   </profile>   <otherdata>     <birthdate>       <xsl:apply-templates select="combined/loadeddata/newbirthdate"/>     </birthdate>     <favoritebooks>       <xsl:for-each select="/combined/profile/otherdata/favoritebooks/book">         <book>           <id>             <xsl:value-of select="id"/>           </id>           <description>             <xsl:value-of select="description"/>             <xsl:apply-templates select="/combined/loadeddata/booksupdate/book[@id='']" />           </description>         </book>       </xsl:for-each>     </favoritebooks>   </otherdata> </xsl:template> </xsl:stylesheet> 

how can closer want get? advise me book jump start, because w3schools tutorials useless :(

how's this?

according logic described, description not "empty" rather "book3" (empty string merged "book3").

<!-- root , static content --> <xsl:template match="/">     <xsl:apply-templates select='combined/profile' /> </xsl:template>  <!-- identity/copy, tweaks --> <xsl:template match='node()|@*'>      <!-- copy node -->     <xsl:copy>          <!-- add in attributes -->         <xsl:apply-templates select='@*' />          <!-- either apply same treatment child nodes, or special -->         <xsl:choose>              <!-- use updated birthdate -->             <xsl:when test='name() = "birthdate"'>                 <xsl:value-of select='/combined/loadeddata/newbirthdate' />             </xsl:when>              <!-- merge book descriptions -->             <xsl:when test='name() = "description"'>                 <xsl:value-of select='concat(., /combined/loadeddata/booksupdate/book[@id = current()/../id]/booktext)' />             </xsl:when>              <!-- or keep recursing -->             <xsl:otherwise>                 <xsl:apply-templates select='node()' />             </xsl:otherwise>         </xsl:choose>     </xsl:copy>      <!-- if we've done books, add in in loaded data not original data -->     <xsl:if test='name() = "book" , not(count(following-sibling::book))'>         <xsl:variable name='orig_book_ids'>             <xsl:for-each select='../book'>                 <xsl:value-of select='concat("-",id,"-")' />             </xsl:for-each>         </xsl:variable>         <xsl:apply-templates select='/combined/loadeddata/booksupdate/book[not(contains($orig_book_ids, concat("-",@id,"-")))]' mode='new_books' />     </xsl:if> </xsl:template>  <!-- new books --> <xsl:template match='book' mode='new_books'>     <book>         <id><xsl:value-of select='@id' /></id>         <description>new <xsl:value-of select='booktext' /></description>     </book> </xsl:template> 

you can run @ this xmlplayground session (see output source).


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -