Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2016, 06:12 AM
Registered User
 
Join Date: Dec 2016
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT replace tags

Hi,
i'm new here, so be gentle :)
i have one program that generates XML for our clients.
here is the part of the code
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" exclude-result-prefixes="php">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>

<xsl:template match="/">


<products>
<xsl:for-each select="objects/object">
<xsl:element name="product"><xsl:attribute name="st"><xsl:value-of select="position()"/></xsl:attribute>
<xsl:element name="productID"><xsl:value-of select="sku"/></xsl:element>
<xsl:element name="productName"><xsl:value-of select="name"/></xsl:element>
<xsl:element name="url"><xsl:value-of select="product_url"/></xsl:element>
<xsl:element name="description"><xsl:value-of select="description"/></xsl:element>
</products>
</template>
Problem is that i have HTML tags in variable description. Here is the output that i have now:

HTML Code:
<p> TEXT1 TEXT1TEXT1TEXT1TEXT1TEXT1TEXT1</p> <p> TEXT1TEXT1TEXT1TEXT1TEXT1TEXT1TEXT1TEXT1 </p> <p> TEXT1TEXT1TEXT1TEXT1TEXT1TEXT1TEXT1 </p> <p> <ul> </p> <p> <li>TEXT1TEXT1TEXT1</li> </p> <p> <li>TEXT1TEXT1TEXT1</li> </p> <p> <li>TEXT1TEXT1</li> </p> <p> <li>TEXT1 </li> </p> <p> <li>TEXT1TEXT1</li> </p> <p> <li>TEXT1TEXT1</li> </p> <p> <li>TEXT1TEXT1</li> </p> <p> </ul> </p> <p> </br> </p> <p> TEXT1TEXT1</br> </p> <p> <ul> </p>  <p> </ul>  
As you can see i have way too many <p> and </p> tags, so i would like to remove all of them from variable description. How can i do that?

Thanks
 
Old December 1st, 2016, 06:26 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to show us your input, your desired output, and your actual output.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
  Spam!  
Old December 1st, 2016, 06:46 AM
Registered User
 
Join Date: Dec 2016
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
here is my output:
Code:
 <productID>BBB1111SKU</productID>
      <productName>product</productName>
      <url>http://my.domain.com</url>
      <description>&lt;p&gt; text text text &lt;/p&gt; &lt;p&gt; . &lt;/p&gt; &lt;p&gt; text text text&lt;/p&gt; &lt;p&gt; text text text &lt;/p&gt; &lt;p&gt; text text text &lt;/p&gt; &lt;p&gt; text text text &lt;/p&gt; &lt;p&gt; text text text &lt;/p&gt; &lt;p&gt; text text text &lt;/p&gt; &lt;p&gt; text text text &lt;/p&gt; &lt;p&gt;text text text &lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;
Desired output:
Code:
 <productID>BBB1111SKU</productID>
      <productName>product</productName>
      <url>http://my.domain.com</url>
      <description> text text texttext text text text text text  text text text  text text text  text text text text text text  text text text  text text text
So i would need to remove all &lt;/p&gt; and &lt;p&gt;

Thank you
 
Old December 1st, 2016, 01:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You still haven't shown us your input.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 2nd, 2016, 03:29 PM
Registered User
 
Join Date: Dec 2016
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What do you mean with my input?
Variables are coming in from mysql table.

Thank you
 
Old December 2nd, 2016, 06:37 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your input is an XML document containing elements like objects, object, sku, name, product_url. Or if it isn't, then your stylesheet makes no sense at all.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 5th, 2016, 05:31 AM
Registered User
 
Join Date: Dec 2016
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
thank you for your reply.
I'm running this script in Magento administration. I've bought a plugin that takes prices of my products from mysql table and put it in xml for our clients.
 
Old December 5th, 2016, 08:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

And the plugin is invoking your transformation? Then it's supplying some XML to that transformation, and we need to know what the XML is. If you don't know, you can find out by adding <xsl:copy-of select="/"/> to your XSLT code, which will copy the input unchanged to the output.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 5th, 2016, 10:21 AM
Registered User
 
Join Date: Dec 2016
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here you go. Here is the relevant code. thank you for this

Code:
 <object>
  <sku>product id</sku>
  <created_at>2016-10-10 04:44:38</created_at>
  <qty>3</qty>
  <final_price>76.6700</final_price>
  <status>Enabled</status>
  <manufacturer>vendor</manufacturer>
  <manufacturer_raw>645</manufacturer_raw>
  <name>speaker</name>
  <image>http://url.to.my.com/media/catalog/product/c/s/url-test-v1_product_02_normal.jpg</image>
  <small_image>http://url.to.my.com//media/catalog/product/c/s/url.test-v1_product_02_normal.jpg</small_image>
  <description>					<p>					Speaker test x2 Speaker test x2					</p>									<p>					Speaker test x2					</p>									<p>					Speaker test x2					</p>									<p>					Speaker test x2</br>Speaker test x2						<p>					  					</p>									<p>					  				</p>									<p>					 Speaker test x2					</p>									<p>					Speaker test x2					</p>									<p>					</ul>					</p>									<p>					</br></br>					</p>									<p>					Speaker test x2					</p>									<p>										</p>				</description>
  <msrp>91.0000</msrp>
  <tax_percent>22</tax_percent>
  <product_url>http://url.to.my.com/url.html</product_url>
  <price>93.5374</price>
  <attribute_set_name>Default</attribute_set_name>
  <images>
   <image>
    <position>1</position>
    <url>http://url.to.my.com/media/catalog/product/c/s/url_.jpg</url>
    <id>6433</id>
   </image>
  </images>
  <stock>
   <qty>3</qty>
  </stock>
  <entity_id>7086</entity_id>
 </object>
</objects>
 
Old December 5th, 2016, 10:59 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

That can't be the input to the transformation because it isn't well-formed XML.

My guess is that there's a CDATA section in there.

But dammit, why should I have to guess?

Even your stylesheet isn't well-formed, the end tags don't match up.





Similar Threads
Thread Thread Starter Forum Replies Last Post
strip html tags with xslt smiter XSLT 4 June 19th, 2012 08:34 AM
Mutiple tags in XSLT Divya XSLT 6 April 20th, 2010 11:29 AM
xslt for tags dnandhak XSLT 4 May 23rd, 2008 05:57 AM
Replacing tags within xml using xslt patelgaurav85 XSLT 0 June 19th, 2007 12:34 PM
XSLT output with tags trinkets XSLT 22 November 14th, 2006 11:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.