Same xpath works on command line but not in xslt - xslt

I'm a beginning user of xslt and xpath. Using an xpath on a command line (Ubuntu 14.04) with a xml file works, but the very same xpath in an xslt file returns nothing. I'm working with Juniper Junos xml files. Any suggestions?
Thanks,
George
The xml file begins with:
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/12.3R8/junos">
<interface-information xmlns="http://xml.juniper.net/junos/12.3R8/junos-interface" junos:style="normal">
<physical-interface>
<name>fe-0/1/0</name>
<logical-interface>
<name>fe-0/1/0.0</name>
...
The command line that works in Ubuntu 14.04 is:
xpath -e "/rpc-reply/interface-information/physical-interface/logical-interface/name" interfaces.xml
The xslt file is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Interfaces</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Name</th>
</tr>
<xsl:for-each select="/rpc-reply/interface-information/physical-interface/logical-interface">
<tr>
<td><xsl:value-of select="name"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

The problem here is quite simple. You are not using namespaces in your XPaths. Apparently your command line utility doesn't care, or evaluates XPaths based on the elements' QNames ignores default namespaces and does some other non-standard handling of namespaces as well.
The solution:
Declare prefixes at the top of your stylesheet:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:nbase="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:junosi="http://xml.juniper.net/junos/12.3R8/junos-interface"
>
use those prefixes in your XPaths:
<xsl:for-each select="/nbase:rpc-reply/junosi:interface-information
/junosi:physical-interface/junosi:logical-interface">
<tr>
<td><xsl:value-of select="junosi:name"/></td>
</tr>
</xsl:for-each>

Related

How to transform all same element in a node

In below xml file contain many author elements. I have mentioned in xslt and display the first element only. I want to show all author elements. Kindly provide the xslt coding for element not in the text will be shown in browser.
XML Code:
<?xml version="1.0" encoding="US-ASCII" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="view.xsl"?>
<!DOCTYPE WileyML3G [
<!ENTITY % wileyml3g.ent SYSTEM "http://v.wiley.com:3535/dtds/wileyml3g/wiley.ent">
%wileyml3g.ent;
]>
<bibliography xml:id="aic16349-bibl-0001" style="numbered" cited="no">
<title type="main">REFERENCE<!--<QUERY xml:id="Q2"><p>References "3–35" were not cited anywhere in the text. Please provide a citation. Alternatively, delete the items from the list.</p></QUERY>--></title>
<bib xml:id="aic16349-bib-0001">
<citation type="journal" xml:id="aic16349-cit-0001"><!--<QUERY xml:id="Q3"><p>Reference "1" is not cited in the text. Please indicate where it should be cited; or delete from the reference list.</p></QUERY>-->
<author><familyName>Deer</familyName> <givenNames>TR</givenNames></author>, <author><familyName>Provenzano</familyName> <givenNames>DA</givenNames></author>, <author><familyName>Hanes</familyName> <givenNames>M</givenNames></author>, et al. <articleTitle>The Neurostimulation Appropriateness Consensus Committee (NACC) Recommendations for Infection Prevention and Management</articleTitle>. <journalTitle>Neuromodulation.</journalTitle> <pubYear year="2017">2017</pubYear>;<vol>20</vol>(<issue>1</issue>):<pageFirst>31</pageFirst>‐<pageLast>50</pageLast>.</citation>
</bib>
XSLT Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>HTML VIEW</h1>
<table border="1" cellpadding="10px">
<tr>
<th>Authors</th>
<th>Year</th>
<th>Article_Title</th>
<th>Journal_Title</th>
<th>Volume</th>
<th>Issue</th>
<th>First_Page</th>
<th>Last_Page</th>
</tr>
<xsl:for-each select="bibliography/bib/citation">
<tr>
<td><span style="background-color:skyblue;"><xsl:value-of select="author"/></span>, </td>
<td><xsl:value-of select="pubYear"/></td>
<td width="75%"><xsl:value-of select="articleTitle"/></td>
<td width="25%"><xsl:value-of select="journalTitle"/></td>
<td><xsl:value-of select="vol"/></td>
<td><xsl:value-of select="issue"/></td>
<td><xsl:value-of select="pageFirst"/></td>
<td><xsl:value-of select="pageLast"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
You have tagged the question as XSLT 2.0 and if you really use an XSLT 2.0 processor and use version="2.0" in your stylesheet then <xsl:value-of select="author"/> would output all selected author child elements and you could even use <xsl:value-of select="author" separator=", "/> to have the different authors separated by ,. If you use XSLT 1.0 then use <xsl:appy-templates select="author"/> and <xsl:template match="author"><xsl:if test="position() > 1">, </xsl:if></xsl:value-of select="."/></xsl:template> or use xsl:for-each if you prefer that.
As #Martin suggestion if you really want to use 1.0 then you have to go with for-each author like:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>HTML VIEW</h1>
<table border="1" cellpadding="10px">
<tr>
<th>Authors</th>
<th>Year</th>
<th>Article_Title</th>
<th>Journal_Title</th>
<th>Volume</th>
<th>Issue</th>
<th>First_Page</th>
<th>Last_Page</th>
</tr>
<xsl:for-each select="bibliography/bib/citation">
<tr>
<td>
<span style="background-color:skyblue;">
<xsl:for-each select="author">
<xsl:value-of select="."/>
<xsl:if test="following-sibling::author"><xsl:text>, </xsl:text></xsl:if>
</xsl:for-each>
</span>
</td>
<td><xsl:value-of select="pubYear"/></td>
<td width="75%"><xsl:value-of select="articleTitle"/></td>
<td width="25%"><xsl:value-of select="journalTitle"/></td>
<td><xsl:value-of select="vol"/></td>
<td><xsl:value-of select="issue"/></td>
<td><xsl:value-of select="pageFirst"/></td>
<td><xsl:value-of select="pageLast"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Or it will be done very easy while working with 2.0 like:
Need to change the XSLT version from 1.0 to 2.0
Use #seperator with , in value-of author.
HTML VIEW
Authors
Year
Article_Title
Journal_Title
Volume
Issue
First_Page
Last_Page

Is it possible to get all the xPaths of an XSLT file?

Is is possible to get all the xPaths used in an XSLT file?
For example:
XSLT File:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="/catalog/cd">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
As output I want a list like this:
/catalog/cd
/catalog/cd/title
/catalog/cd/artist
Maybe by making a custom post processor that outputs a line everytime an xPaths has matched/not been matched?
Ideas are welcome, because I'm very hopeless :)
Thanks!
You can use this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:text>/</xsl:text>
<xsl:value-of select="local-name(.)"/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
It's difficult because template rule matching in XSLT is very dynamic: if you have a template with match="cities" that calls xsl:apply-templates, and if you have another template with match="city", then the XSLT compiler can't know that there is a path /cities/city.
Internally Saxon has the capability to build a "path map" rather as you describe, and it's used to implement "document projection" in XQuery, but it's of very little use in XSLT because of dynamic template rule despatch.
So static analysis to determine the paths isn't going to get you very far. You also suggest that dynamic analysis - capturing the paths visited at run-time - might also be of interest. In principle you can do that in Saxon with a TraceListener. The difficulty is in determining exactly what you mean by "visited" - for example do you consider xsl:copy-of select="/" as visiting every node in the document, or only the root node?

How to display the text of a hyperlink in SharePoint

I am trying to display the text of a hyperlink. The hyperlink is obtained from a query, and its column type is Publishing Hyperlink.
This code <xsl:value-of select="$Link" /> display the full link
File 1
How can I just display the text of the link?
File
Is there some code like <xsl:text-of select="$Link" /> or something else?
You can use
<xsl:value-of select="YOUR ANCHOR TEXT TAG IN XML HERE"/>
Replace your link text tag name you have given in your xml file with "YOUR ANCHOR TEXT TAG IN XML HERE"
This will display the anchor text for sure.
See the example below
XML Code
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<link>http://www.google.com</link>
<text>google</text>
</cd>
<cd>
<link>http://www.yahoo.com</link>
<text>yahoo</text>
</cd>
</catalog>
XSLT Code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Anchor Link</th>
<th style="text-align:left">Anchor Text</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="link"/></td>
<td><xsl:value-of select="text"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Please click on the url to see output
https://i.stack.imgur.com/Dp24I.jpg
According DataSource details:
Source
Using:
<xsl:text-of select="$Link.desc" />
Should work.

XML namespace reference in XSL stylesheet problem

I am trying to extract data out of the following XML fragment:
<?xml version= "1.0" ?>
<Stmts xmlns="http://tempuri.org/Statement.xsd" Generation="2011-08-01T12:41:41" >
<StatementDetail AccountStatus="Open" CompanyID="" TransactionCount="182" >
<Transactions>
<Manual.../>
...
</Transactions>
</StatementDetail>
</Stmts>
Notice that the element has a xmlns attribute.
When I try to use the following XSL I get no data.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>CabCharge</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Batch</th>
<th>TransNo</th>
</tr>
<xsl:for-each select="Stmt/StatementDetail/Transactions/Manual">
<tr>
<td><xsl:value-of select="#Batch"/></td>
<td><xsl:value-of select="#TransNo"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
BUT! If I remove the XMLNS attribute from the element, I do get data.
What do I need to specifiy in the XSL to recognise the namespace???
Thanks.
Make sure to declare the default namespace of the document in your stylesheet, like:
<xsl:stylesheet version="1.0"
xmlns="http://tempuri.org/Statement.xsd"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Otherwise the XSLT processor will consider the elements referenced in your stylesheet as belonging to default namespace null.
Moreover, if you want the namespace exlcuded from the output document, you need to declare null namespace for the root literal element html, like:
<xsl:template match="/">
<html xmlns="">
<!-- your stuff -->
</html>
</xsl:template>
Do note also that:
In the xsl:for-each you are selecting the wrong element (Stmt in place of Stmts)
The attributes Batch and TransNo do not exist in your input documents.

Change font in XML using XSLT

I'm new to XSLT. I'm trying to change the font size of a specific text in XML file using XSLT. For eg- I have the CDCatalog.xml file with following data.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="cdcat.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist><SmallText>Bob Dylan</SmallText><LineBreak/>*</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
and the cdCat.XSL file is-
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:include href="cdCatalog.xsl" /> <!-- I added this -->
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title" />
</td>
<td>
<xsl:value-of select="artist" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I added a new xsl file cdCatalog.XSL file with following details-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="LineBreak">
<br/>
</xsl:template>
<xsl:template match="Superscript">
<sup>
<xsl:value-of select="."/>
</sup>
</xsl:template>
<xsl:template match="SmallText">
<font size="1">
<xsl:value-of select="."/>
</font>
</xsl:template>
</xsl:stylesheet>
and included this file in the CDCat.xsl file.and added the tags - <smallText>, <LineBreak> in the CdCatalog.xml file. now when I open the xml file i dont see the LineBreak nor the font size difference. Can anyone please suggest if I'm missing something.
Thanks in advance
Sai
You need to use apply-templates to indicate where your template matches should take effect.
XML says nothing about presentation, that's the whole point. It's a data format.
If you want your XSLT to output to something where presentation matters I suggest you transform to HTML and get let HTML/CSS handle the styling.
Having seen your actual code now (hint: use the formatting when creating questions) don't use the font tag. What you want semantically and in practice is just headers <h1>, <h2>, <h3> etc, and I'd still suggest you add a CSS link in there. Oh and <xsl:output method="html" />
In-between these two opening tags:
<html>
<body>
...I'd place a link to a style sheet that defines the font sizes. Alternatively (and useful if you want a self contained HTML file to email around) you could put a style block there instead.