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.
Related
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.
Is it possible to have out-of-sequence tags within XSLT using 1.0? My initial guess is not, as it breaks the rules of XML.
Consider XML data that has X elements, and I want to split those X entries into blocks of 3 within individual <div> blocks. What I would like to do is something this, but obviously it is completely invalid code...
<div>
<xsl:for-each select="mydata">
<xsl:value-of select="myvalue"/><br/>
<xsl:if test="(position() mod 3)=0">
</div> <!-- This is invalid -->
<div> <!-- This is invalid -->
</xsl:if>
</xsl:for-each>
</div>
So for 8 elements, the example result would be
<div>
value1<br/>
value2<br/>
value3<br/>
</div>
<div>
value4<br/>
value5<br/>
value6<br/>
</div>
<div>
value7<br/>
value8<br/>
</div>
If the above is simply not possible (as I suspect it is not), can somebody suggest an acceptable way to group them like this?
(Please note, this must be an XSLT 1.0 solution)
What you're trying to do is possible, but it isn't a good idea. This is a better approach:
<xsl:apply-templates select="mydata[position() mod 3 = 1]" mode="group" />
<!-- Separate templates -->
<xsl:template match="mydata" mode="group">
<div>
<xsl:apply-templates select=". | following-sibling::mydata[position() < 3]" />
</div>
</xsl:template>
<xsl:template match="mydata">
<xsl:value-of select="myvalue"/><br/>
</xsl:template>
JLRishe shows you the solution.
Your problem is that you are thinking of your stylesheet as writing start and end tags. That's not what XSLT does: it writes a tree. You can't write half a node to the result tree. Think nodes, not tags.
When you have problems like this in which the output structure doesn't exactly match the input structure, another useful rule of thumb is that the structure of the stylesheet should reflect the tree structure of the output, not that of the input. Don't think "what shall I do with the next ABC input node", but rather "I need to generate an XYZ node in the result tree, how shall I compute its content?".
I have in an input file:
<a></a>
<b/>
<c>text</c>
I need to converting this to string. Using transformer I am getting below output:
<a/> <!-- Empty tags should not collapse-->
<b/>
<c>text</c>
If I use xslt and output method is "HTML", I get the below output:
<a></a> <!-- This is as expected-->
<b></b> <!-- This is not expected-->
<c>text</c>
I want the structure same as in input file. It is required in my application since I need to calculate index and it will be very difficult to change the index calution logic.
What would be the correct XSLT to use?
What XSLT processor? XSLT is merely a language to transform xml so "html output" is dependent on the processor.
I'm going to guess this first solution is too simple for you but i've had to use this to avoid processing raw html
<xsl:copy-of select="child::node()" />
as this should clone the raw input.
In my case, I have used the following to extract all nodes that had the raw attribute:
<xsl:for-each select="xmlData//node()[#raw]">
<xsl:copy-of select="child::node()" />
</xsl:for-each>
Other options:
2) Add an attribute to each empty node depending on what you want it to do later ie role="long", role="short-hand".
3)
Loop through each node (xsl:for-each)
<xsl:choose>
<xsl:when test="string-length(.)=0"> <!-- There is no child-->
<xsl:copy-of select="node()" />
</xsl:when>
<xsl:otherwise>
...whatever normal processing you have
</xsl:otherwise>
4) Redefine your problem. Both are valid XHTML/XML, so perhaps your problem can be reframed or fixed elsewhere.
Either way, you may want to add more information in your question so that we can reproduce your problem and test it locally.
P.S. Too much text/code to put in a comment, but that's where this would belong.
A possible alternative is to use disable-output-escaping like this:
<xsl:text disable-output-escaping="yes"><a></a></xsl:text>
But I understand that this is a dirty solution...
I have a requirement where the xml might have one or more services (name might be different), and I am having a content which should have all of these available services from the xml something like below
<li>CMS</li>
<li>DIS</li>
but above I have hardcoded the a tag content and href since I know these are the values, but in real time I would not be knowing these names, so how to set href and anchor tag contents based on xml values?
So far I got the below for-each statement, which gets me all the service names from the xml
<xsl:variable name="number">
<xsl:number/>
</xsl:variable>
<xsl:for-each select="csmclient/product/domainmetadata/domainmetadata_service">
<li><xsl:value-of select="#name"/><xsl:value-of select="position()"/></li>
</xsl:for-each>
.
.
.
<!--far below end-->
<xsl:for-each select="domainmetadata/domainmetadata_service">
<h3>Service Name: <span style="color:#328aa4"><a name="_ser{$number}" href="#_top"><xsl:value-of select="#name"/></a></span></h3>
.
.
.
</xsl:for-each>
but it does not seem to work, it gives me all my services but the link does not work. Any other ideas?
Note: I took help from this question link which had a similar requirement.
There is a xslt function generate-id() which gives and unique textual identifier for any node in the xml.
http://www.w3.org/TR/xslt#function-generate-id
Use something like below, should work
<xsl:for-each select="csmclient/product/domainmetadata/domainmetadata_service">
<li><xsl:value-of select="#name"/></li>
</xsl:for-each>
<xsl:for-each select="domainmetadata/domainmetadata_service">
<h3>Service Name: <span style="color:#328aa4"><a name="{generate-id()}" href="#_top"><xsl:value-of select="#name"/></a></span></h3>
</xsl:for-each>
<li><xsl:value-of select="#name"/><xsl:value-of select="position()"/></li>
but it does not seem to work ...
What you are trying to write is this (not discussing at all if this uniquely identifies the node):
<li>
<a href="#_ser{$number}{#name}{position()}"/>
</li>
Is there a way to use just one way of using a variable within XSLT?
For example:
<xsl:variable name="myvar" select="title" />
Which selects the title from my xml, then I would like to use it within the template:
<h2 title="{$myvar}">{$myvar}</h2>
However it only shows the title attribute.
And when I do it like this:
<h2 title="<xsl:value-of select='$myvar'>"><xsl:value-of select='$myvar'></h2>
It only works the other way around.
So my question:
Is there one way that works for both attribute and content?
Yes, you can avoid AVT:
<h2>
<xsl:attribute name="title">
<xsl:value-of select=$myvar/>
</xsl:attribute>
<xsl:value-of select=$myvar/>
</h2>
But I think it's simpler using AVT and xsl:value-of properly:
<h2 title="{$myvar}">
<xsl:value-of select=$myvar/>
</h2>
XSLT uses XML syntax, which is why you can't have an xsl:value-of instruction inside an attribute: hence the AVT notation using curly braces. It would be nice if the language allowed curly braces inside text nodes as well, but it doesn't.