I'm new to XSLT. I'm trying to change the font size of a specific text in XML file using XSLT. For eg- I have the CDCatalog.xml file with following data.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="cdcat.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist><SmallText>Bob Dylan</SmallText><LineBreak/>*</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
and the cdCat.XSL file is-
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:include href="cdCatalog.xsl" /> <!-- I added this -->
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title" />
</td>
<td>
<xsl:value-of select="artist" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I added a new xsl file cdCatalog.XSL file with following details-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="LineBreak">
<br/>
</xsl:template>
<xsl:template match="Superscript">
<sup>
<xsl:value-of select="."/>
</sup>
</xsl:template>
<xsl:template match="SmallText">
<font size="1">
<xsl:value-of select="."/>
</font>
</xsl:template>
</xsl:stylesheet>
and included this file in the CDCat.xsl file.and added the tags - <smallText>, <LineBreak> in the CdCatalog.xml file. now when I open the xml file i dont see the LineBreak nor the font size difference. Can anyone please suggest if I'm missing something.
Thanks in advance
Sai
You need to use apply-templates to indicate where your template matches should take effect.
XML says nothing about presentation, that's the whole point. It's a data format.
If you want your XSLT to output to something where presentation matters I suggest you transform to HTML and get let HTML/CSS handle the styling.
Having seen your actual code now (hint: use the formatting when creating questions) don't use the font tag. What you want semantically and in practice is just headers <h1>, <h2>, <h3> etc, and I'd still suggest you add a CSS link in there. Oh and <xsl:output method="html" />
In-between these two opening tags:
<html>
<body>
...I'd place a link to a style sheet that defines the font sizes. Alternatively (and useful if you want a self contained HTML file to email around) you could put a style block there instead.
Related
Is is possible to get all the xPaths used in an XSLT file?
For example:
XSLT File:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="/catalog/cd">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
As output I want a list like this:
/catalog/cd
/catalog/cd/title
/catalog/cd/artist
Maybe by making a custom post processor that outputs a line everytime an xPaths has matched/not been matched?
Ideas are welcome, because I'm very hopeless :)
Thanks!
You can use this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:text>/</xsl:text>
<xsl:value-of select="local-name(.)"/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
It's difficult because template rule matching in XSLT is very dynamic: if you have a template with match="cities" that calls xsl:apply-templates, and if you have another template with match="city", then the XSLT compiler can't know that there is a path /cities/city.
Internally Saxon has the capability to build a "path map" rather as you describe, and it's used to implement "document projection" in XQuery, but it's of very little use in XSLT because of dynamic template rule despatch.
So static analysis to determine the paths isn't going to get you very far. You also suggest that dynamic analysis - capturing the paths visited at run-time - might also be of interest. In principle you can do that in Saxon with a TraceListener. The difficulty is in determining exactly what you mean by "visited" - for example do you consider xsl:copy-of select="/" as visiting every node in the document, or only the root node?
I am trying to display the text of a hyperlink. The hyperlink is obtained from a query, and its column type is Publishing Hyperlink.
This code <xsl:value-of select="$Link" /> display the full link
File 1
How can I just display the text of the link?
File
Is there some code like <xsl:text-of select="$Link" /> or something else?
You can use
<xsl:value-of select="YOUR ANCHOR TEXT TAG IN XML HERE"/>
Replace your link text tag name you have given in your xml file with "YOUR ANCHOR TEXT TAG IN XML HERE"
This will display the anchor text for sure.
See the example below
XML Code
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<link>http://www.google.com</link>
<text>google</text>
</cd>
<cd>
<link>http://www.yahoo.com</link>
<text>yahoo</text>
</cd>
</catalog>
XSLT Code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Anchor Link</th>
<th style="text-align:left">Anchor Text</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="link"/></td>
<td><xsl:value-of select="text"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Please click on the url to see output
https://i.stack.imgur.com/Dp24I.jpg
According DataSource details:
Source
Using:
<xsl:text-of select="$Link.desc" />
Should work.
I'm trying to retrieve a certain value from an XML document and output that value into a new XML document - the source XML is full of unused data, I only need the specific part.
Source XML :-
<dpp:Programme xmlns:dpp="http://www.digitalproductionpartnership.co.uk/ns/as11/2012" xmlns:itv="http://dpp.itv.com/timecodes/v1">
<dpp:Editorial>
<dpp:SeriesTitle>test</dpp:SeriesTitle>
<dpp:ProgrammeTitle>test</dpp:ProgrammeTitle>
<dpp:EpisodeTitleNumber>test</dpp:EpisodeTitleNumber>
<dpp:ProductionNumber>2/1993/0022#001</dpp:ProductionNumber>
<dpp:Synopsis>None</dpp:Synopsis>
<dpp:Originator>None</dpp:Originator>
<dpp:CopyrightYear>2013</dpp:CopyrightYear>
</dpp:Editorial>
<dpp:Technical>
<dpp:ShimName>UK DPP HD</dpp:ShimName>
<dpp:Video>
<dpp:VideoBitRate unit="Mbps">100</dpp:VideoBitRate>
<dpp:VideoCodec>AVCI</dpp:VideoCodec>
<dpp:VideoCodecParameters>High 4:2:2 level 4.1</dpp:VideoCodecParameters>
<dpp:PictureFormat>1080i50 16:9</dpp:PictureFormat>
<dpp:AFD>10</dpp:AFD>
<dpp:PictureRatio>16:9</dpp:PictureRatio>
<dpp:ThreeD>false</dpp:ThreeD>
<dpp:ProductPlacement>false</dpp:ProductPlacement>
<dpp:FPAPass>Not tested</dpp:FPAPass>
</dpp:Video>
<dpp:Audio>
<dpp:AudioSamplingFrequency unit="kHz">48</dpp:AudioSamplingFrequency>
<dpp:AudioBitDepth>24</dpp:AudioBitDepth>
<dpp:AudioCodecParameters>PCM</dpp:AudioCodecParameters>
<dpp:AudioTrackLayout>EBU R 123: 4b</dpp:AudioTrackLayout>
<dpp:PrimaryAudioLanguage>eng</dpp:PrimaryAudioLanguage>
<dpp:SecondaryAudioLanguage>zxx</dpp:SecondaryAudioLanguage>
<dpp:TertiaryAudioLanguage>eng</dpp:TertiaryAudioLanguage>
<dpp:AudioLoudnessStandard>EBU R 128</dpp:AudioLoudnessStandard>
</dpp:Audio>
<dpp:Timecodes>
<dpp:LineUpStart>09:58:00:00</dpp:LineUpStart>
<dpp:IdentClockStart>09:59:20:00</dpp:IdentClockStart>
<dpp:Parts>
<dpp:Part>
<dpp:PartNumber>1</dpp:PartNumber>
<dpp:PartTotal>1</dpp:PartTotal>
<dpp:PartSOM>10:30:41:11</dpp:PartSOM>
<dpp:PartDuration>00:00:30:13</dpp:PartDuration>
</dpp:Part>
</dpp:Parts>
<dpp:TotalNumberOfParts>1</dpp:TotalNumberOfParts>
<dpp:TotalProgrammeDuration>00:00:30:13</dpp:TotalProgrammeDuration>
</dpp:Timecodes>
<dpp:AccessServices>
<dpp:AudioDescriptionPresent>false</dpp:AudioDescriptionPresent>
<dpp:ClosedCaptionsPresent>false</dpp:ClosedCaptionsPresent>
<dpp:OpenCaptionsPresent>false</dpp:OpenCaptionsPresent>
<dpp:SigningPresent>No</dpp:SigningPresent>
</dpp:AccessServices>
<dpp:Additional>
<dpp:CompletionDate>2014-01-07</dpp:CompletionDate>
<dpp:TextlessElementExist>false</dpp:TextlessElementExist>
<dpp:ProgrammeHasText>true</dpp:ProgrammeHasText>
<dpp:ProgrammeTextLanguage>eng</dpp:ProgrammeTextLanguage>
<dpp:AssociatedMediaFilename>2-1993-0022-001.mxf</dpp:AssociatedMediaFilename>
<dpp:MediaChecksumType>MD5</dpp:MediaChecksumType>
<dpp:MediaChecksumValue>6154fd9cf312492e2dea68bee656ded7</dpp:MediaChecksumValue>
</dpp:Additional>
<dpp:ContactInformation>
<dpp:ContactEmail>None</dpp:ContactEmail>
<dpp:ContactTelephoneNumber>None</dpp:ContactTelephoneNumber>
</dpp:ContactInformation>
</dpp:Technical>
<itv:AdditionalTimeCodes>
<itv:Element>
<itv:ElementType>Essence</itv:ElementType>
<itv:ElementSOM>10:30:41:11</itv:ElementSOM>
<itv:Duration>00:00:30:13</itv:Duration>
<itv:Fade>false</itv:Fade>
<itv:Mix>false</itv:Mix>
<itv:Property>Essence</itv:Property>
</itv:Element>
</itv:AdditionalTimeCodes>
</dpp:Programme>
This is the XSL I have created :-
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="Programme/Technical/Timecodes">
<tr>
<td>
<xsl:value-of select="TotalProgrammeDuration"/>
</td>
</tr>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
But all I'm getting returned is a blank page?
All I need is the timecode value (TotalProgrammeDuration) from Programme/Technical/Timecodes
What am I doing wrong? (I'm very new to this - if you can't rell already)
J.
The elements in your input XML have a namespace. You need to declare this namespace in your XSLT stylesheet too - and prefix any element names you mention.
Namespaces are an important concept in XSLT (as with XML technologies in general) so I recommend you spend some time understanding the basics. For instance, start with a previous answer of mine.
Stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dpp="http://www.digitalproductionpartnership.co.uk/ns/as11/2012">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="dpp:Programme/dpp:Technical/dpp:Timecodes">
<tr>
<td>
<xsl:value-of select="dpp:TotalProgrammeDuration"/>
</td>
</tr>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Also note that you are obviously outputting XHTML. Then, it makes more sense to set
<xsl:output method="text">
to
<xsl:output method="html">
Further, indent="yes" only makes sense when used with html, not with text.
Below is a second attempt at writing your stylesheet that uses separate templates (which is generally a better idea than using xsl:for-each).
Stylesheet (a better approach)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dpp="http://www.digitalproductionpartnership.co.uk/ns/as11/2012">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="dpp:Timecodes">
<tr>
<td>
<xsl:value-of select="dpp:TotalProgrammeDuration"/>
</td>
</tr>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Output
<html xmlns:dpp="http://www.digitalproductionpartnership.co.uk/ns/as11/2012">
<body>
<tr>
<td>00:00:30:13</td>
</tr>
</body>
</html>
You are missing namespace declarations:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dpp="http://www.digitalproductionpartnership.co.uk/ns/as11/2012" exclude-result-prefixes="dpp">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="dpp:Programme/dpp:Technical/dpp:Timecodes">
<tr>
<td>
<xsl:value-of select="dpp:TotalProgrammeDuration"/>
</td>
</tr>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Input xml is
<getArtifactContentResponse>
<return>
<![CDATA[
<metadata>
<overview>
<name>scannapp</name>
<developerId>developer702</developerId>
<stateId>2</stateId>
<serverURL>abc.com</serverURL>
<id>cspapp1103</id>
<description>scann doc</description>
<hostingTypeId>1</hostingTypeId>
</overview>
</metadata>
]]>
</return>
</getArtifactContentResponse>
Below is the stylesheet which I have developed. I am able to retrieve the XML inside Cdata but not able to fetch the elements value.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:output method="html" encoding="utf-8" omit-xml-declaration="no" indent="no"/>
<xsl:template match="/">
<html>
<body>
<h1>Company Details</h1>
<table border="1">
<tr>
<th>name</th>
<th>developerId</th>
<th>Id</th>
</tr>table
<xsl:variable name ="data" select="//getArtifactContentResponse/return/node()" />
<tr>
<td>
<xsl:value-of select="$data/metadata/overview/name" disable-output-escaping="yes"/>
</td>
<td>
<xsl:value-of select="$data/metadata/overview/developerId" />
</td>
<td>
<xsl:value-of select="$data/metadata/overview/Id" />
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output coming as
<html><body><h1>Company Details</h1><table border="1"><tr><th>name</th><th>developerId</th><th>serverURL</th></tr>table
<tr><td></td><td></td><td></td></tr></table></body></html>
Expected output
<html><body><h1>Company Details</h1><table border="1"><tr><th>name</th><th>developerId</th><th>serverURL</th></tr>table
<tr><td>scannapp</td><td>developer702</td><td>cspapp1103</td></tr></table></body></html>
I want to take the value name,developerId,Id and print to HtML. How to do that Please help me. Using XSLT version1.0.
AFAIK, there's no way to parse CDATA as XML.
Using an extension function to parse the section as text would be nice, but not really necessary. Here's an example that extracts the three items you're after:
...
<xsl:variable name ="cdata" select="/getArtifactContentResponse/return" />
<name>
<xsl:value-of select="substring-before(substring-after($cdata, '<name>'), '</name>')"/>
</name>
<developerId>
<xsl:value-of select="substring-before(substring-after($cdata, '<developerId>'), '</developerId>')"/>
</developerId>
<id>
<xsl:value-of select="substring-before(substring-after($cdata, '<id>'), '</id>')"/>
</id>
...
Is there any way to import stylesheets after checking some conditions?
Like,if the value of variable $a="1" then import 1.xsl or else import 2.xsl.
Hi All, Is there any way to import
stylesheets after checking some
conditions?
Like,if the value of variable $a="1"
then import 1.xsl or else import
2.xsl.
No, the <xsl:import> directive is only compile-time.
In XSLT 2.0 one can use the use-when attribute for a limited conditional compilation.
For example:
<xsl:import href="module-A.xsl"
use-when="system-property('xsl:vendor')='vendor-A'"/>
The limitations of the use-when attribute are that there is no dynamic context when the attribute is evaluated -- in particular that means that there are no in-scope variables defined.
A non-XSLT solution is to dynamically change the href attribute of the <xsl:import> declaration before the transformation is invoked:
Parse the xsl stylesheet as an XML file
Evaluate the condition that determines which stylesheet should be imported.
Set the value of the href attribute of the <xsl:import> declaration to the URI of the dynamically determined stylesheet-to-be-imported.
Invoke the transformation with the in-memory xsl stylesheet that was just modified.
I know this post is old, but I want to share my opinion.
Each display could use one template instead of two. The value display will be change with a VB application.
breakfast_menu.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="conditionDisplay.xsl" ?>
<data>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
<display>1</display>
</data>
In this file, I imported my displays and with a condition I tell the template what I need.
conditionDisplay.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:import href="display1.xsl"/>
<xsl:import href="display2.xsl"/>
<xsl:template match="/">
<xsl:variable name="display"><xsl:value-of select= "data/display"/></xsl:variable>
<xsl:choose>
<xsl:when test="$display='1'">
<xsl:call-template name="display1" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="display2 />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
display1.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="display1">
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="data/breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
</p>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
display2.xsl:
<?xml version="1.0" encoding="UTF-8"?>futur
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="display2">
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#222222">
<xsl:for-each select="data/breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
</p>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I genuinely apologize for my terrible English. It will be better to the next post and I hope will help someone as I think it's not the best solution.