I want to use my xml file to create different xsl pages.
i.e
<movies>
<movie>
<name>Shark tank</name>
<movie>
<movie>
<name>Tank Shark</name>
<movie>
<movies>
I want to display this information using xsl however i want the first movie on it's on page with a link to another page displaying movie2. Is this possible and if so how do i go about getting my desired outcome ?
On the main page you're going to want something like this at the beginning of the main.xsl file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0" xml:lang="en"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:import href="./file1.xsl"/>
<xsl:import href="./file2.xsl"/>
Unless you are using all xml file with one main xsl file, then add these global variables example after the imports:
<xsl:variable name="MOVIE" select="document('movies.xml')"/>
with that you can call these variables as such:
<a href="http://iheartsharks.com">
<xsl:value-of select="$MOVIE/movie[name = "'Shark tank']"/>
</a>
Related
I have come across this problem while doing a home project and I honestly do not know how to solve it.
The goal is, using XSLT, get a certain attribute from my XML and add it to href.
<Video videoID="v0001" imageLink="IMG/VIDEO_IMG/v0001_img.jpg"/>
Then, by applying the XSLT, that looks kinda like this:
<xsl:template match="/">
<xsl:for-each select="Video"/>
<a href="X">
<img src="Y"/>
</a>
</xsl:template>
where X is the imageLink from the XML and Y is gonna be the local link to the XHTML page of that single video.
<!--The goal is getting something like this after using XSLT-->
<a href="VIDEOS/video_v0001.xhtml">
<img class="User" src="../IMG/VIDEO_IMG/v0001_img.png" alt="VideoImage"/>
</a>
Assume that your XML input is:
<?xml version="1.0" encoding="UTF-8"?>
<Videos>
<Video videoID="v0001" imageLink="IMG/VIDEO_IMG/v0001_img.jpg"/>
<Video videoID="v0002" imageLink="IMG/VIDEO_IMG/v0002_img.jpg"/>
</Videos>
Then the XSLT script performing your task can be:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xhtml" doctype-public="-//W3C//DTD XHTML 1.1//EN"
doctype-system= "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="Videos/Video">
<a href="VIDEOS/video_{#videoID}.xhtml">
<img class="User" src="../{substring-before(#imageLink, '.')}.png"
alt="VideoImage"/>
</a>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:transform>
As you can see, there should be 2 Attribute Value Templates ({...}),
one drawing the content from source videoID attribute and the other -
from imageLink.
In the second case the content to be copied is limited to the part
before the dot, because in this case file name extension is different.
Note that I used method="xhtml" and both doctype attributes for XHTML
page. Maybe in your case they should be different.
For a working example see http://xsltransform.net/jxWYjVH/1
To get a full HTML page, add <head> tag and other HTML stuff, according
to your needs, e.g. a stylesheet for User class.
This is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="staff.xsl"?>
<st>
<employee>
<name>
<firstname>Clay</firstname>
<lastname>Hansen</lastname>
</name>
<jobtitle>Professor</jobtitle>
<department>Department: Communication and Marketing</department>
<office>Office: CH-H 111</office>
<phone>Phone: (847)257-1234</phone>
<email>Email: clay.h#abc.com</email>
<profile>
<html xmlns="http://www.w3.org/1999/xhtml">
<img src="C:\Users\username\Desktop\im.jpg" /> <!-- added closing tag by edit -->
</html>
</profile>
</employee>
</st>
here is my xml and extrenal xsl code. When I add an image, it does not display my data in tabular form. It displays it like words in a sentence. How can I solve it?
This is because xsl:value-of returns the concatenated text nodes of a element's subtree with the markup removed (in your case there is none). You could copy the img node like this:
<xsl:for-each select="employee">
<td>
<xsl:copy-of select="profile//xhtml:img"></xsl:copy-of>
</td>
</xsl:for-each>
Note, that you have to declare the xhtml namespace in your stylesheet to process the img nodes:
xmlns:xhtml="http://www.w3.org/1999/xhtml"
Is it possible for XSLT preserve anchors and other embedded HTML tags within XML?
Background: I am trying to convert an HTML document into XML with an XSL stylesheet using XSLT. The original HTML document had content interspersed with anchor tags (e.g. Some hyperlinks here and there). I've copied that content into my XML, but the XSLT output lacks anchor tags.
Example XML:
<?xml version="1.0" ?>
<observations>
<observation>Hyperlinks disappear.</observation>
</observations>
Example XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/html">
<xsl:output method="html" indent="yes" encoding="UTF-8"/>
<xsl:template match="/observations">
<html>
<body>
<xsl:value-of select="observation"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html xmlns="http://www.w3.org/1999/html">
<body>Hyperlinks disappear.</body>
</html>
I've read a few similar articles on stackoverflow and checked out the Identity transform page on wikipedia; I started to get some interesting results using xsl:copy-of, but I don't understand enough about XSLT to get all of the words and tags embedded within each XML element to appear in the resulting HTML. Any help would be appreciated.
Write a separate template to match a elements, copy their attributes and content.
What is wrong with your approach? In your code,
<xsl:value-of select="observation"/>
simply sends to the output the string value of the observation element. Its string value is the concatenation of all text nodes it contains. But you need not only the text nodes in it, but also the a elements themselves.
The default behaviour of an XSLT processor is to "skip" element nodes, because of a built-in template. So, if you do not mention a in a template match, it is simply ignored and only its text content is output.
Stylesheet
Note: This stylesheet still relies on the default behaviour of the XSLT processor to some extent. The order of events will resemble the following:
The template where match="/observations" is matched. It adds html
and body to the output. Then, a template rule must be found for the
content of observations. A built-in template matches observation,
does nothing with it, and looks for a template to process its content.
For the a element, the corresponding template is matched, with
copies the element and attributes. Finally, a built-in template copies
the text nodes inside observation and a.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/observations">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="a">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML Output
<html>
<body>Hyperlinks disappear.
</body>
</html>
I am trying to do a simple transform of XML using XSLT to generate HTML, but I'm having difficulties and I can't seem to figure out what the problem is. Here is a sample of the XML I am working with:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="C:\Users\cgubata\Documents\Digital Measures\jcamp_fac_ex_xslt.xsl"?>
<Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2012-02-27">
<Record userId="310106" username="jcamp" termId="453" dmd:surveyId="1154523">
<dmd:IndexEntry indexKey="COLLEGE" entryKey="School of Business" text="School of Business"/>
<dmd:IndexEntry indexKey="DEPARTMENT" entryKey="Accountancy" text="Accountancy"/>
<dmd:IndexEntry indexKey="DEPARTMENT" entryKey="MBA" text="MBA"/>
<PCI id="11454808064" dmd:lastModified="2012-02-08T13:17:39">
<PREFIX>Dr.</PREFIX>
<FNAME>Julia</FNAME>
<PFNAME/>
<MNAME>M.</MNAME>
<LNAME>Camp</LNAME>
<SUFFIX/>
<ALT_NAME>Julia M. Brennan</ALT_NAME>
<ENDPOS/>
All I want to do is have the value for some of the nodes to be displayed in HTML. So for example, I might want the PREFIC, FNAME, LNAME nodes to display as "Dr. Julia Camp" (no quotes - I'll do styling later). Here's the XSL that I am using:
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="jcamp_fac_ex.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<xsl:value-of select="/Data/Record/PCI/PREFIX"/>
</xsl:template>
</xsl:stylesheet>
From what I been researching, that should show the value of that PREFIX field. But instead, it is outputting all of the values from all of the nodes (so if there are 4000 nodes with a text value, I am getting 4000 values returned in HTML). My goal will be to pull out the values from certain nodes, and I will probably arrange them in a table.
How do I pull out the values from a specific node? Thanks in advance.
Well, I can't reproduce your symptoms. When I test what you've posted it doesn't produce any output at all. Which looks correct because your xpath is testing the wrong namespace. You need to add in your xslt a namespace-prefix mapping for the http://www.digitalmeasures.com/schema/data namespace, and then use it in the value-of xpath. Like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata"
xmlns:dm="http://www.digitalmeasures.com/schema/data">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<xsl:value-of select="/dm:Data/dm:Record/dm:PCI/dm:PREFIX"/>
</xsl:template>
</xsl:stylesheet>
I'm afraid you've fallen into the number one XSLT trap for beginners: we see this question at least once a day on this forum. Your elements are in a namespace and your stylesheet is trying to match nodes in no namespace.
What am I doing wrong? It shows up as a blank page. I don't know a whole lot of xslt so I'm just testing it right now. If I can't get this to show up though then I can't really test how it works.
My XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="q3.xsl"?>
<expenseReport>
<companyInfo>
<name>John Doe</name>
<email>JDoe#company.com</email>
<empNum>1</empNum>
<companyCode>10010011</companyCode>
</companyInfo>
</expenseReport>
xslt
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Test</h1>
<xsl:value-of select="expenseReport/companyInfo/name"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I can't even get that <h1> to show up, what's going on?
Please use --allow-file-access-from-files if you are trying to view local files from Chrome. They are very strict with local file access so do not rely on this option when you deploy your site. Only use it for development! I just checked it with version 14.0.385.202 and it worked.
Other than this your .xslt is just fine!