 |
| 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
|
|
|
|

February 19th, 2010, 08:45 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Unable to transform content
Hi,
I have some content as below:
<p style="FONT-FAMILY: Arial Black">sample text</p>
There can be many paragraph tags with different fonts. Now, I want to use XSLT to apply a unique font across all paragraphs.
I used below two codes but it doesn't do what i have expected.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xslutput method="html" omit-xml-declaration="yes"/>
<xsl:template match="p">
<p style="FONT-FAMILY: Arial">
<xsl:value-of select="." disable-output-escaping="yes"/>
</p>
</xsl:template>
</xsl:stylesheet>
--------------------------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xslutput method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:for-each select="p">
<p style="FONT-FAMILY: Arial">
<xsl:value-of select="."/>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Can any one please fix any bug here?
Thanks in advance
|
|

February 19th, 2010, 09:04 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Not really because you have failed to say a) What you want the output to look like, and b) why the code above isn't working, i.e. what is it outputting and what is wrong with it.
|
|

February 19th, 2010, 09:06 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
examples
please check the below examples:
Input: <p style="FONT-FAMILY: Arial Black">sample text</p>
Output: <p style="FONT-FAMILY: Arial">sample text</p>
Input: <p style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Arial Black" xmlns="http://www.w3.org/1999/xhtml">sample text</p>
Output: <p style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Arial" xmlns="http://www.w3.org/1999/xhtml">sample text</p>
I want to convert those inputs to the correspondign outputs. THe only change I want to do is FONT-FAMILY and apply Arial. But I'm unable to do that through XSLT though I have tried many variations :(
Please let me know, if you do require any other information.
Thanks again
|
|

February 19th, 2010, 09:08 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
issue with my experiments
basically whatever the code i have written is not getting applied and not effective to my input. The output is coming as just like inpue without changing Font-family.
|
|

February 19th, 2010, 09:11 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Looks like a basic namespace problem: some of your examples show <p> elements in a namespace, others give no indication of whether the element is in a namespace or not. If you want to match p elements in the XHTML namespace, you need
<xsl:template match="h:p" xmlns:h="http://www.w3.org/1999/xhtml">
More typically, you will promote the namespace declaration to the xsl:stylesheet element.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

February 19th, 2010, 09:18 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
my bad
I believe you asked me try this code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="html" indent="yes" cdata-section-elements="script"/>
<xsl:template match="h:p" xmlns:h="http://www.w3.org/1999/xhtml">
<p style="FONT-FAMILY: Arial">
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
When I tried this with the input
<p xmlns="http://www.w3.org/1999/xhtml">Sample Text</p>
<p style="FONT-SIZE: 9pt; FONT-FAMILY: Arial Black" xmlns="http://www.w3.org/1999/xhtml">Sample Text</p>
<p style="FONT-SIZE: 10pt; FONT-FAMILY: Garamond" xmlns="http://www.w3.org/1999/xhtml">Sample Text</p>
I have got below output:
<p>Sample Text</p><p style="FONT-SIZE: 9pt; FONT-FAMILY: Arial Black">Sample Text</p><p style="FONT-SIZE: 10pt; FONT-FAMILY: Garamond">Sample Text</p>
It seems to have removed namespace in the output
Can you please look into this where i'm doing mistake? This has really troubled me the whole day :(
Many Thanks
|
|

February 19th, 2010, 09:49 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
If you want your output elements to use the same name and namespace as the input, use <xsl:copy> rather than <p>. Alternatively, if you want to output a <p> element in the XHTML namespace, use <p xmlns="http://www.w3.org/1999/xhtml">. The system can't guess that you want your new <p> elements in a namespace - you have to tell it!
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

February 19th, 2010, 10:23 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
fixed
This below code fixed my issue
<xsl:stylesheet version="1.0" exclude-result-prefixes="xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:output omit-xml-declaration="yes" method="xml" indent="yes" cdata-section-elements="script"/>
<xsl:template match="/">
<div xmlns="" class="content">
<xsl:apply-templates/>
</div>
</xsl:template>
<!-- match all nodes with an attribute style -->
<xsl:template match="@style">
<xsl:element name="{name()}">
<xsl:attribute name="style">font-family:Arial</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- match all nodes with an attribute font-->
<xsl:template match="@font">
<xsl:element name="{name()}">
<xsl:attribute name="style">font-family:Arial</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- match all tables -->
<xsl:template match="table">
<table xmlns="" cellspacing="0" cellpadding="0" border="1">
<xsl:attribute name="style">width:100%;</xsl:attribute>
<xsl:apply-templates/>
</table>
</xsl:template>
<!-- match all that's left -->
<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
|
|

February 19th, 2010, 10:53 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
That works does it? I find that hard to believe but I'm glad for you if it does.
Note, match="@font" shouldn't match elements WITH the font attribute, it should actually match the font attribute its self. match="*[@font]" would match elements with the font attribute.
|
|

February 19th, 2010, 11:02 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
didn't work
it didn't work ; i actually misread after posting here :( my output is below:
i tried your <xsl:template match="*[@font]"> but still the output is same
it really is eating me up!!
<p xmlns="http://www.w3.org/1999/xhtml">sample</p>
<p xmlns="http://www.w3.org/1999/xhtml">
<style style="font-family:Arial" xmlns=""></style>sample</p>
<p xmlns="http://www.w3.org/1999/xhtml">sample</p>
<p xmlns="http://www.w3.org/1999/xhtml"> </p>
|
|
 |