I am trying to use the code below to select all values of notableWork, so for each notableWork get its value and get the next one etc. It is sort of working as two "Notable Work:" are appearing just not the values of notable work.
<xsl:for-each select="notableWork">
<p>
Notable Work: <xsl:value-of select="notableWork" />
</p>
</xsl:for-each>
The other issue I am having is with images. I have been messing around with the img tag trying variations of it, below is just what I have ended up with. I have an element called imaged which contains a value of image2.jpg, image4.jpg etc. I cannot seem to get it to work :P Just wondering what the correct syntax is, using google I found variations of so really not sure.
<img src="<xsl:value-of select="image" />" />
Thanks :)
<xsl:for-each select="notableWork">
<p>
Notable Work: <xsl:value-of select="notableWork" />
</p>
</xsl:for-each>
can never work unless you have a nesting like notableWork/notableWork. You already selected notableWork so you cant select it again. So you should work with the node, the current node. Which is XSLT: ., current() or XPath: self::node(). All 3 can be used for the same effect.
Notable Work: <xsl:value-of select="." />
or
Notable Work: <xsl:value-of select="current()"/>
To set the attribute of a image tag you can use
<img>
<xsl:attribute name="src">
<xsl:value-of select="images" />
</xsl:attribute>
</img>
or
<img src="{image}"/>
Related
The following template fixes the img/src attribute and is there since years:
<xsl:template match="xh:img/#src">
<xsl:attribute name="src">
<xsl:value-of select="
if( string-length(substring-before(substring-after(
subsequence(parent::node()/following-sibling::comment(),1,1),'src="'),'.eps')) > 0 )
then
concat('images/',tokenize(concat(substring-before(substring-after(
subsequence(parent::node()/following-sibling::comment(),1,1),'src="'),'.eps'),'.png'),'/')[last()])
else
data(self::node())"/>
</xsl:attribute>
</xsl:template>
Now I added the following template to move width and height to style:
<xsl:template match="xh:img">
<img style="width:{#width}; height:{#height};">
<xsl:copy-of select="#*[not(name()='width' or name()='height')]"/>
</img>
</xsl:template>
The second one works but it "disables" the first one.
If I comment the second one, the first works.
Is there a way to merge them?
I really don't have experience with XSL so any help would be appreciated.
This is because you are using xsl:copy-of to copy attributes in the template matching xh:img. This will not apply any matching templates, but just copy them exactly.
Simply change to using xsl:apply-templates...
<xsl:apply-templates select="#*[not(name()='width' or name()='height')]"/>
However, you might need to add an additional template to match attributes other than src should you wish them to still be created too.
<xsl:template match="#*">
<xsl:copy />
</xsl:template>
Hi and happy year to everyone !
Once again, I need your help with a transformation in XSLT.
I have an XML-TEI file with a lot of pb elements. For instance :
<pb ed="bnf" id="f77.image.r"/>
I have also in the same file one link element :
<link id="bnf" target="http://test.com:/12148/btv1b90621925/"/>
My transformation in xslt should not only transform the <pb> element to look like this in HTML : [77r] but the [77r] should also be a hyperlink.
Now, it only does the first stuff :
<xsl:template match="pb[#ed='bnf']"><span class="pb">
<xsl:text> [</xsl:text>
<xsl:value-of select="substring-after(substring-before(#id, '.image.'), 'f')"/><xsl:value-of select="substring-after(#id, '.image.')">
</xsl:value-of>
<xsl:text></xsl:text>
<xsl:apply-templates/>
<xsl:text>] </xsl:text>
</span>
</xsl:template>
How can I reach the link element and do so the [f77r] takes the value of the link and concat it with the id of the pb element ? Every pb should be a hyperlink like this (with the end changing for each pb) :
http://test.com:/12148/btv1b90621925/f77.image.r
Thank you so much for your help :)
Best wishes,
Micha
It looks like you could use a key here, to look up the link elements by there id attribute.
<xsl:key name="links" match="link" use="#id" />
Then you can use the key to get the target of the link when you construct the href for the hyperlink
<a href="{key('links', #ed)/#target}{#id}">
Note the double-use of Attribute Value Templates to create the attribute. You could also write this if you wanted
<a href="{concat(key('links', #ed)/#target, #id)}">
Try this XSLT template
<xsl:template match="pb[#ed='bnf']">
<span class="pb">
<a href="{concat(key('links', #ed)/#target, #id)}">
<xsl:text> [</xsl:text>
<xsl:value-of select="substring-after(substring-before(#id, '.image.'), 'f')"/><xsl:value-of select="substring-after(#id, '.image.')">
</xsl:value-of>
<xsl:apply-templates/>
<xsl:text>] </xsl:text>
</a>
</span>
</xsl:template>
This should output the following:
<span class="pb">
[77r]
</span>
<!-- Section: /report/detail -->
<xsl:template match="/report/detail">
<div href="./lf_web" style="left: 0.0ex; position: absolute"><xsl:value-of select="./lf_po"/></div>
<br/>
</xsl:template>
In this code I am trying to use the content of the taq element "./lf_web".
Can any one help me please.
It is in and .xsl file and linked to .xml data source
You should tag your question with XSL instead of div and href which are not important to your question.
One way is to first retrieve the value in a variable. I like that way because you can use functions:
<xsl:variable name="website_uri" as="xs:string">
<xsl:value-of select="info/uri"/>
</xsl:variable>
<xsl:variable name="protocol" as="xs:string">
<xsl:value-of select="substring-before($website_uri, '://')"/>
</xsl:variable>
And I show how you use a variable: with a $ sign before the name of the value. However, to do so inside a different attribute, you need to put it between curly brackets;
...
Now I think you can do the query you're trying to achieve inline too, but I don't see a good example for it.
I am very new to xml and xslt transformation. I need some advice.
Basically I have managed to display a range of images from my XML page using XLTR Transformation.
The original xml has no ID's associated with each image, I cant alter the xml. Therefore I was wondering if there was a way in xslt to add an ID attribute to each image before it is outputted to the browser? As I want to be able to control each element/image individually using CSS.
I hope that makes sense?
The xslt is as follows.
<xsl:for-each select=" cars/car_type">
<!-- <h5> <xsl:value-of select="car-type"/> </h5> -->
<div class ="images">
<img>
<xsl:attribute name="src">
<xsl:value-of select="image"/>
</xsl:attribute>
</img>
</div>
</xsl:for-each>
Thanks again.
You can use generate-id (http://www.w3.org/TR/xslt#function-generate-id) function to generate an id for an input node e.g.
<xsl:for-each select="cars/car_type">
<div class="images">
<img src="{image}" id="{generate-id(image)}"/>
</div>
</xsl:for-each>
When you create the CSS elsewhere in your stylesheet you simply need to make sure you process the image elements again and use generate-id again.
So i have some style content saved in the node #style and i want to call it to form the style attribute of the <span> tag. it is also insufficient, so i want to append the margin-left:700px as part of the style attribute for the <span> tag. is this do-able?
by the way, i cannot edit the database which the xsl is calling as it is provided by the client
<span style="<xsl:copy-of select="Root/Node/MyFile/MyStyle/#style" margin-left:700px">
<xsl:copy-of select="Root/Node/MyFile/MyCaption/Caption"/>
</span>
Btw, i tried the above and Altova complains that the Character < from <xsl... is grammatically unexpected.
Your quotes are probably screwing up the XSLT, try this:
<span>
<xsl:attribute name="style">
<xsl:copy-of select="Root/Node/MyFile/MyStyle/#style" />
<xsl:text> margin-left:700px</xsl:Text>
</xsl:attribute>
<xsl:copy-of select="Root/Node/MyFile/MyCaption/Caption"/>
</span>
I cant test it at the moment though...
You might want to learn about "Attribute Value Templates" in this case. This allows you to 'embed' the code directly in an attribute.
<span style="{Root/Node/MyFile/MyStyle/#style}" margin-left:700px">
<xsl:value-of select="Root/Node/MyFile/MyCaption/Caption"/>
</span>
The curly braces { and } here indicate the Attribute Value Template (AVT) and indicate the code inside the braces should be executed to get the value.
You will need to ensure your xpath expression 'Root/Node/MyFile/MyStyle/#style' does indeed return the correct value though.
You can't nest like that, but you can do something similar:
Say you have a variable called 'style_var' with some CSS:
<xsl:variable name="style_var">color: blue;</xsl:variable>
And an HTML element, span:
<span>
...
</span>
I'm fairly certain you can do this:
<span>
<xsl:attribute name="style">
<xsl:value-of select="$style_var" />
<xsl:text>margin-left: 100px;</xsl:Text>
</xsl:attribute>
</span>
And you can probably swap out the variable selector for the node selector you were using above.