38692

XSL to XML - How do i do this?

Question:

This is my xml file:

<HarperCollins> <BookComingSoon> <BookDescription> After a family tragedy... <NewParagraph/> Holden Cameron was addicted... <NewParagraph/> When equipment, treasure... </BookDescription> <BookComingSoon> </HarperCollins>

and I must change it with XML to look like this(the BookDescription part):

<blurb>

Example text example text example text, woo!

More example text. Amazing!

</blurb>

how do i do this with XSL(1.0)?

this is what i got so far:

<blurb> <xsl:for-each select="BookDescription">

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

</xsl:for-each> </blurb>

<strong>EDIT</strong>

this is the full xml doc:

<?xml version="1.0"?> <HarperCollins> <BookComingSoon> <Book>Night Diver</Book> <By>Elizabeth Lowell</By> <Pages>368</Pages> <CoverImage>nd-el.jpg</CoverImage> <OnSale>4/8/2014</OnSale> <ForFansOf>Romance</ForFansOf> <ForFansOf>Suspense</ForFansOf> <BookDescription> After a family tragedy... <NewParagraph/> Holden Cameron... <NewParagraph/> When equipment, treasure... </BookDescription> </BookComingSoon> <BookComingSoon> <Book>Under a Silent Moon</Book> <By>Elizabeth Haynes</By> <Pages>352</Pages> <CoverImage>uasm-eh.jpg</CoverImage> <OnSale>4/15/2014</OnSale> <ForFansOf>Suspense</ForFansOf> <ForFansOf>Thriller</ForFansOf> <BookDescription> In the crisp, ... <NewParagraph /> As DCI Louisa Smith ... </BookDescription> </BookComingSoon> </HarperCollins>

The for-each I use is to do both the nodes.

Answer1:

Could go simpler. You can avoid the empty template by never matching the NewParagraph element in the first place.

<xsl:template match="/"> <blurb><xsl:apply-templates match="//BookDescription/text()" /></blurb> </xsl:template> <xsl:template match="BookDescription/text()">

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

</xsl:template>

Answer2:

For-each is totally overused :-) Try this:

<xsl:template match="BookComingSoon"> <blurb><xsl:apply-templates match="BookDescription" /></blurb> </xsl:template> <xsl:template match="BookDescription/text()">

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

</xsl:template> <xsl:template match="NewParagraph" />

The first one will kick the processing of the book description. The second pulls out all the text and surrounds it with

, and the last suppresses the unneeded element.

Recommend

  • Woocommerce cannot see products in wp-admin
  • Watch form validity from controller
  • Need Woocommerce to only allow 1 product in the cart. If a product is already in the cart and anothe
  • DevTools listening on ws://127.0.0.1:57671/devtools/browser/8a586f7c-5f2c-4d10-8174-7a7bf50e49b5 wit
  • Is it possible to have dynamically generated query names in ColdFusion?
  • Updating table from JSON/MYSQL with jQuery
  • NHibernate.Mapping Exception. No persister for Namespace.className
  • jQuery UI and Prototype conflict
  • Sending Python function as Boost.Function argument
  • How does JIT compilation actually execute the machine code at runtime?
  • Rename Element and retain attributes
  • How to display rendered html content in text widget of tkinter in python 3.4.x
  • XSLT escaping >
  • Java Socket Reuse
  • Empty Div Causing Very Strange Behavior
  • How can we use EXSLT without downloading its source?
  • Giving uploaded images a unique name for mysqli
  • Css display none not working
  • finding maximum depth of chapter
  • Testing Meteor packages with Velocity?
  • Duplicate Element x number of times with XSLT
  • Help with mod_rewrite
  • how to display   in Mozilla using XSL.
  • XSLT foreach repeating nodes to flat
  • print() is showing quotation marks in results
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Bad request using file_get_contents for PUT request in PHP
  • Android fill_parent issue
  • Get object from AWS S3 as a stream
  • SSO with signing and signature validation doesn't work
  • MySQL WHERE-condition in procedure ignored
  • Deserializing XML into class C#
  • ORA-29908: missing primary invocation for ancillary operator
  • Web-crawler for facebook in python
  • How to get next/previous record number?
  • trying to dynamically update Highchart column chart but series undefined
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • costura.fody for a dll that references another dll
  • java string with new operator and a literal