xslt is pretty new for me. Is it possible to do something similar to my code below. I know it is possible in other template languages.
<div class="<xsl:if test="position()=1">myclass</xsl:if>">Hello</div>
You could wrap an xsl:attribute in an xsl:if...
<div>
<xsl:if test="position()=1">
<xsl:attribute name="class">myclass</xsl:attribute>
</xsl:if>
<xsl:text>Hello</xsl:text>
</div>
Also, in XSLT 2.0, you can write the xsl:attribute like this:
<xsl:attribute name="class" select="'myClass'"/>
Another XSLT 2.0 option, if you don't mind having an empty class="", is to use an if in an AVT (Attribute Value Template):
<div class="{if (position()=1) then . else ''}">...</div>
The then may vary depending on context.
It should be something like this:
<xsl:variable name="myclass" select="variablenode" />
<div class="adf">
<xsl:if test="yournode[position()=1]">
<xsl:value-of select="$myclass"/>
</xsl:if>
Hello</div>
But please give us your source XML, the XSLT you have so far and the expected output. Otherwise we can only guess.
Related
I would like to set an xsl variable based on a condition. The xsl below is not currently working for me. I've been able to make two different matchers (<xsl:template match="rule/condition" and <xsl:template match="condition/condition") which enables me to put the ;display:none on just condition/condition matches but that results in the template being duplicated except for the one part of ;display:none. Guess I'm under the impression that I should be able to dynamically set a variable based on a condition but maybe my impression is wrong?
<xsl:template match="condition">
<xsl:variable name="display">
<xsl:if test='name(..)=condition'>;display=none</xsl:if>
</xsl:variable>
<div style="{$divIndent}{$display}">
<a href="javascript:void(0)" style="{$expandPosition}" onclick="expandContents(this)">
<span class="glyphicon glyphicon-plus"></span>
</a>
<condition type="<xsl:value-of select="#type"/>"><br />
<xsl:apply-templates select="expression" />
<xsl:apply-templates select="condition" />
</condition>
</div>
</xsl:template>
<xsl:if test='name(..)=condition'>;display=none</xsl:if>
This asks if the name of the parent is equal to the value of the child whose name is condition. You probably want to know if the name of the parent is the literal value "condition":
<xsl:if test='name(..)="condition"'>;display=none</xsl:if>
However, it might be more idiomatic to write:
<xsl:if test='parent::condition'>;display=none</xsl:if>
Note also that display:none is valid css, display=none prbably won't work.
Try below code by adding attribute style
<div><xsl:attribute name ="style"><xsl:if test='name(..)=condition'>display=none;</xsl:if></xsl:attribute></div>
I have a XML structure like this, just some valid XML structure mixed with HTML tags. I am trying to match <p>MyHeader</p> in the section and set it to empty.
That is after running the XSLT on this structure, i don't want to print the <p>MyHeader</p> tag.
<abstract>
<section>
<p>MyHeader</p>
<p> other content in section </p>
<h1> other content in section </h1>
</section>
</abstract>
Here's what I am trying in the XSL
<xsl:template match="abstract/section/p">
<xsl:choose>
<xsl:when test="text() ='MyHeader'"></xsl:when>
<xsl:otherwise><xsl:apply-templates /></xsl:otherwise>
</xsl:choose>
</xsl:template>
any ideas on what's wrong with my code above? I dont see <p>MyHeader</p> tag being stripped out.
<xsl:template match="abstract/section/p">
<xsl:choose>
<xsl:when test="text() ='MyHeader'"></xsl:when>
<xsl:otherwise><xsl:apply-templates /></xsl:otherwise>
</xsl:choose>
</xsl:template>
what's wrong with my code
Nothing wrong with the code shown -- the problem is in the code you haven't shown to us.
Guessing:
The template isn't selected for execution.
There is an explicit <xsl:copy-of> selecting this p element.
Recommendation: just use:
<xsl:template match="abstract/section/p[.='MyHeader']"/>
I'm learning on the fly, and I have commented C# code with /// that generates xml after:
csc Class1.cs /out:Class1Docs /rescurse:*.cs /doc:Class1Doc.xml
My xsl file (the relevant part) is like so:
<xsl:for-each select="doc">
<br>
<xsl:value-of select="members/member"/>
</br>
<br>
<xsl:value-of select="summary"/>
</br>
</xsl:for-each>
</body>
</html>
</xsl:template>
The problem is that I need to output the member, summary, params, and return value.
I only get the 'member/member' returned:
Class1 Class
How do i select the xsl:value of for each tag ?
My xsl file (the relevant part) is
like so:
<xsl:for-each select="doc">
<br>
<xsl:value-of select="members/member"/
</br>
<br>
<xsl:value-of select="summary"/>
</br>
</xsl:for-each>
</body>
</html>
</xsl:template>
This is badly malformed XML!
Let's think that the relevant, well-formed portion is:
<xsl:for-each select="doc">
<br>
<xsl:value-of select="members/member"/>
</br>
<br>
<xsl:value-of select="summary"/>
</br>
</xsl:for-each>
There are many problems with this code:
AFAIK the (x)HTML element br should have no content. Maybe you wanted:
<xsl:value-of select="members/member"/>
The <xsl:value-of> instruction creates a text node that contains the string value of the first node only, that is specified in the XPath expression in the select attribute.
Probably you wanted:
<xsl:copy-of select="members/member/text()"/>
.3. Specifying all literal result elements and using <xsl:for-each> within a single template is not a good XSLT programming practice. It is recommended to use more templates and correspondingly, <xsl:apply-templates> instead of <xsl:for-each>.
I can't really post the whole XML example because it's internal info
Then it's easy enough to provide an example that exhibits all the necessary properties of the real data, surely? You can't expect people to help you with one hand tied behind their backs.
Like other responders, I'm guessing, but my guess is that you failed to realise that in XSLT 1.0, the xsl:value-of instruction only outputs the string value of the first node in the sequence. If for some reason you can't move to XSLT 2.0, you need to put the xsl:value-of inside an xsl:for-each or xsl:apply-templates loop.
I have some XSLT that I didn't write but I have to maintain it. At some points it is using generate-id() to create an empty div which is then manipulated by a script further on. Problem is that the div is inside another div (i had thought this was a bit pointless, but acceptable anyway) and both are being given the same id! I tried this with Xalan and then again with the JRE transformer with the same result. I then tried to use the Eclipse/Xalan XSLT debugger and when i am stepping over the thing, it all works correctly...
The XSLT looks like this:
<xsl:template match="listed">
<xsl:variable name="showwhat">
<xsl:call-template name="IdentifyAudience"/>
</xsl:variable>
<xsl:if test="string-length($showwhat) > 0">
<div>
<xsl:attribute name="id">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
<xsl:call-template name="Blah"/>
<xsl:apply-templates/>
</div>
</xsl:if>
</xsl:template>
And the XML fragment is like this:
<listed id='ID39AF705AE17A000F337B000A' mode='html'>
<ul>
<li>Blah blah</li>
</ul>
</listed>
<listed id='ID39AF715892AE000F337B002B' mode='html'>
<ul>
<li>Blech blech.</li>
</ul>
</listed>
I appreciate this is a bit complicated - any clues? Please?
The problem is in the code you haven't show to us.
generate-id() function return an unique identifier string for argument node or the context node otherwise.
So, it's unique for unique node... You are probably calling the p/0 function in two content template with the same context node. How is this posible? Well...
<xsl:call-template name="Blah"/>
call-template instruction does not change the context node
I am trying to create hyperlinks using XML information and XSLT templates. Here is the XML source.
<smartText>
Among individual stocks, the top percentage gainers in the S. and P. 500 are
<smartTextLink smartTextRic="http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=HBAN.O">Huntington Bancshares Inc</smartTextLink>
and
<smartTextLink smartTextRic="http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=EK">Eastman Kodak Co</smartTextLink>
.
</smartText>
I want the output to look like this, with the company names being hyperlinks based on the "smartTextLink" tags in the Xml.
Among individual stocks, the top percentage gainers in the S.&P. 500 are Eastman Kodak Co and Huntington Bancshares Inc.
Here are the templates that I am using right now. I can get the text to display, but not the hyperlinks.
<xsl:template match="smartText">
<p class="smartText">
<xsl:apply-templates select="child::node()" />
</p>
</xsl:template>
<xsl:template match="smartTextLink">
<a>
<xsl:apply-templates select="child::node()" />
<xsl:attribute name="href">
<xsl:value-of select="#smartTextRic"/>
</xsl:attribute>
</a>
</xsl:template>
I have tried multiple variations to try to get the hyperlinks to work correctly. I am thinking that the template match="smartTextLink" is not being instantiated for some reason. Does anyone have any ideas on how I can make this work?
EDIT: After reviewing some of the answers, it is still not working in my overall application.
I am calling the smartText template from within my main template
using the following statement...
<xsl:value-of select="marketSummaryModuleData/smartText"/>
Could this also be a part of the problem?
Thank you
Shane
Either move the xsl:attribute before any children, or use an attribute value template.
<xsl:template match="smartTextLink">
<a href="{#smartTextRic}">
<xsl:apply-templates/>
</a>
</xsl:template>
From the creating attributes section of the XSLT 1 spec:
The following are all errors:
Adding an attribute to an element after children have been added to it; implementations may either signal the error or ignore the attribute.
Try this - worked for me:
<xsl:template match="smartText">
<p class="smartText">
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="smartTextLink">
<a>
<xsl:attribute name="href">
<xsl:value-of select="#smartTextRic"/>
</xsl:attribute>
<xsl:value-of select="text()"/>
</a>
</xsl:template>
Trick is - <xsl:attribute> first, before you do any other processing.
Marc