Solved

Getting attribute Values using xslt

Posted on 2016-09-02
4
60 Views
Last Modified: 2016-09-08
Hi All,

I am trying to get the attribute value from 100's of xml files using below code..
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="2.0" xmlns:saxon="http://sf.net/saxon" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="saxon xs">
	<xsl:output indent="yes" method="html" encoding="UTF-8" />
	<xsl:strip-space elements="*" />
	<xsl:template match="/">
		<html>
			<body>
				<ol>
					<xsl:for-each select="//fileref/@href">
						<xsl:variable name="file" select="concat('../input.files/',.)" />
						<xsl:message>Processing ...<xsl:value-of select="." /></xsl:message>
						<xsl:for-each select="document($file)/distinct-values(//trans-unit/@id)">
							<xsl:sort order="ascending" select="." />
							<li>
								<xsl:value-of select="." />
							</li>
						</xsl:for-each>
					</xsl:for-each>
				</ol>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Open in new window

However, I am not getting any results.

Can you please suggest what's gone wrong here?
Thanks,
Shail
0
Comment
Question by:Shailesh Shinde
  • 2
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41781474
technically I don't see a direct issue
but it is very hard to comment on this... and it is really easy for you to debug

1. first test the outer loop
				<ol>
					<xsl:for-each select="//fileref/@href">
						<xsl:variable name="file" select="concat('../input.files/',.)" />
							<li>
								<xsl:value-of select="$file" />
							</li>
						</xsl:for-each>
					</xsl:for-each>
				</ol>

Open in new window


So then you know whether the source XML file already gives you the correct file urls
(you have not added the source XML so I can't test that myself)
0
 
LVL 3

Author Comment

by:Shailesh Shinde
ID: 41781518
Hi Geert,

Yes, I did that and when I changed the xpath something like below it has shown the results.
<xsl:for-each select="document($file)/distinct-values(//@id)">

Open in new window

This is strange as xml structure is quite simple...
<file>
<body>
<group>
<trans-unit id="m1"></trans-unit>
<trans-unit id="m1"></trans-unit>
<trans-unit id="m2"></trans-unit>
<trans-unit id="m2"></trans-unit>
</group>
</body>
</file>

Open in new window

Thanks,
Shail
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 41781544
so... you are proving you question was not detailed enough
Please, in future give ALL information, because only now it is apparent what you should be looking at

if this is working
            <xsl:for-each select="document($file)/distinct-values(//@id)">
                        
and this is not
            <xsl:for-each select="document($file)/distinct-values(//trans-unit/@id)">

it simply means that there are no @id in a <trans-unit> element
I am pretty certain that the XML example you show is not complete
I bet there is an xml namespace declaration binding the default namespace to a namespace different from the NULL namespace

I assume this would work
            <xsl:for-each select="document($file)/distinct-values(//*:trans-unit/@id)">
0
 
LVL 3

Author Closing Comment

by:Shailesh Shinde
ID: 41789351
Yes, this works perfectly
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question