Get text but exclude node if it has a certain child in a for-each loop in XSLT? - xslt

I'm trying to get the following text
Divided into:
Bonaire, Sint Eustatius and Saba (BQ, BES, 535)
Curaçao (CW, CUW, 531)
Sint Maarten (Dutch part) (SX, SXM, 534)
out of this source (excerpt):
<td>
Divided into:<br />
Bonaire, Sint Eustatius and Saba (<tt>BQ</tt>, <tt>BES</tt>, <tt>535</tt>) <sup id="cite_ref-7" class="reference">
<a href="#cite_note-7">
<span>[</span>note 4<span>]</span>
</a>
</sup><br />
Curaçao (<tt>CW</tt>, <tt>CUW</tt>, <tt>531</tt>)<br />
Sint Maarten (Dutch part) (<tt>SX</tt>, <tt>SXM</tt>, <tt>534</tt>)
</td>
This is easily done with <xsl:value select="td[4]"/> (it's the 4th td element, and I'm looping over the surrounded trs).
But I want to exclude the text [note 4], so every a that has span children.
I tried td[4]/node()[not(descendant::span)], but it only left Divided into:. td[4][not(//span)] gives always empty strings.

when you match td[4]/node()[not(descendant::span)] you're matching the forth td that doesn't have a span descendant. Since your td[4] does have a span descendant,you're getting empty results.
What you need is is a template to match the td[4] descendant nodes that does text output:
<xsl:template match="td[4]/node()"> ... <xsl:template> <!-- match descendant nodes of td[4] -->
and another template to specifically catch the span node:
<xsl:template match="span | text()[preceding-sibling::span] | text()[following-sibling::span]"/>

Related

XSLT 1.0 - change the order of lines

I want to change the order of line cac:InvoiceLine depending on this node:
cac:AdditionalItemProperty/cbc:Value
All InvoiceLines that have Item type=RC must be gruop at the end of lines, and all that have CU must be on the top.
If the mentioned values are the only ones you are concerned about, then it seems like you could just sort alphabetically by that value; see xsl:sort. You could just put this inside the xsl:for-each or xsl:apply-templates where you process your invoice lines:
<xsl:sort select="cac:AdditionalItemProperty/cbc:Value" />
On the other hand, if you only want to output only the line items with the mentioned values, you could select them separately. For example, assuming you have a template which matches your invoice lines, you'd first apply it to the 'CU' ones and then to the 'RC' ones:
<xsl:apply-templates select="cac:InvoiceLine[cac:AdditionalItemProperty/cbc:Value='CU']" />
<xsl:apply-templates select="cac:InvoiceLine[cac:AdditionalItemProperty/cbc:Value='RC']" />

How to apply-templates to all templates except a spefic one

In the code bellow I'm applying all the templates to the element chapter (in the end of the code) but I would like to know how it is possible to apply all the templates to this element except a spefic one. In this case it's the element title because I'm already selecting it in the line before and it appears repeated in the html file. Someone?
<xsl:template match="chapter">
<h3>
<a name="{#id}"><xsl:value-of select="title"/></a>
</h3>
<xsl:apply-templates/>
</xsl:template>
Output:
<h3>Title</h3>
Title<br>
Text.
A plain <xsl:apply-templates/> is equivalent to <xsl:apply-templates select="node()" />, i.e. all child nodes. You can exclude certain nodes by using the XPath 2.0 except operator, e.g.
<xsl:apply-templates select="node() except title" />
This would select all child nodes except those that are elements with the name title. If you are only interested in child elements (not text nodes etc.) then you could use * except title instead.
The except operator essentially implements set difference - you're not limited to simple element names on the right, you can use any expression that returns a sequence of nodes, e.g.
node() except (title | div[#class = 'heading'])
X except Y selects all nodes that are in the sequence selected by X and not also in the sequence selected by Y

pugixml: selecting nodes fails

I'm using pugixml to parse the following xml:
<td class="title">
<div class="random" />
Link1
</td>
<td class="title">
<div class="random" />
Link2
</td>
etc...
I want the value of every 'a href' in a td class ="title" (which appears an indeterminate number of times) but only the first such instance.
I am using the following code to try and get these values:
pugi::xpath_node_set link_nodes = list_doc.select_nodes("//td[#class='title']");
for (pugi::xpath_node_set::const_iterator it = link_nodes.begin();it != link_nodes.end();++it)
{
pugi::xpath_node single_link_node = *it;
std::cout << single_link_node.node().select_single_node("//a").node().attribute("href").value()<<std::endl;
}
which doesn't seem to work (it outputs number of times but with a value that doesn't even seem to appear within that element).
Thanks.
"//a" selects all "a" nodes in the document; you probably meant ".//a" that selects all "a" nodes in the subtree.
You can also use one XPath expression instead of multiple:
//td[#class='title']//a[1]
This selects the first tag for each td - i.e. [1] only applies to //a, not to the full expression.

XSL: Calling specific Element from Multiple Instances of one Schema

I created a component named "BusLocationLinks" it stores the name of the business as well as the coordinates for a map I created.
I have nearly 50 business locations with that same schema (BusLocationsLinks) and only want to list the element "Business Name" of all components components of that name. I've tried everything but can not make them all display. ANY ADVICE?
Here's my current code:
<xsl:template name="BusLocationLinks">
<xsl:for-each select="BusLocationLinks/BusinessName">
<li class="active">
<xsl:value-of select="BusinessName" />
</li>
</xsl:for-each>
</xsl:template>
my xml code looks similar to such:
<BusLocationLinks>
<BusinessName>Star Property</BusinessName>
</BusLocationLinks>
Without seeing your XML it is difficult to diagnose the problem. However, it is likely that you have the following structure:
<BusLocationLinks>
<BusinessName>name1</BusinessName>
<BusinessName>name2</BusinessName>
<BusinessName>name3</BusinessName>
</BusLocationLinks>
If that is the case, then you should adjust your XSLT like this:
<xsl:template name="BusLocationLinks">
<xsl:for-each select="BusinessName">
<li class="active">
<xsl:value-of select="." />
</li>
</xsl:for-each>
</xsl:template>
The body of an xsl:for-each instruction resets the context node to be one of the nodes in the selected node set (a different one each time the body of the for-each is evaluated).
In your example, that means that within the body of the for-each, the current node is one of the BusLocationLinks/BusinessName elements you selected. Your loop creates a list-item element for each of them (check your output, I expect you'll see them there) containing the value of the BusinessName child of the context node. The context node matches the expression BusLocationLinks/BusinessName, so you are looking for the values of nodes which match BusLocationLinks / BusinessName / BusinessName. If you don't have any nodes that match the expression BusLocationLinks / BusinessName / BusinessName, you'll be getting empty li elements.
Try <xsl:value-of select="."/>.

a bit foggy about the xpath 'following' axis (as used in xslt)

<a>
<x/>
<m/>
<y/>
<m/>
</a>
Inside a template that matches 'a', I want to match (first) any 'm's before 'y', and then
separately any 'm's after 'y'.
<xsl:apply-templates select="./m[following::y]"/>
is what I thought of, but I can't get it to work, and, further, I can't see how to prevent the template that matches on 'm' from being applied in the normal flow as well as in the particular place i want to insert the m-related content.
Your template looks OK, but are you sure you want to use following? For example, this template:
<xsl:template match="a">
<a><xsl:apply-templates select="m[following::y]"/></a>
<b><xsl:apply-templates select="m[following-sibling::y]"/></b>
</xsl:template>
...applied to the following XML:
<a>
<x/>
<m>match</m>
<y/>
<m>no match</m>
<nested>
<m>match 2</m>
<y/>
</nested>
</a>
...outputs the following result:
<a>matchno match</a>
<b>match</b>
The first apply-templates matches <m>no match</m> because following includes all nodes that are after the context node in document order, which includes the nested <y/>.
The second template matches only siblings.
For completeness, I'll add the following template, which matches only those <m> nodes whose immediate following sibling is a <y>:
<xsl:template match="a">
<a><xsl:apply-templates select="m[following-sibling::*[1][self::y]]"/></a>
</xsl:template>
This template outputs the following, given the above XML:
<a>match</a>