Solved

Getting attribute Values using xslt

Posted on 2016-09-02
4
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
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…
The viewer will learn how to count occurrences of each item in an array.

691 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