[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Retrieving Complete XPATH from XML File


Subject: Re: [xsl] Retrieving Complete XPATH from XML File
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Tue, 09 Nov 2010 09:14:28 +0000

For the first form, try

<xsl:for-each select="//*">
<xsl:value-of select="string-join(ancestor-or-self::*/name(), '/')"/>
</xsl:for-each>

For the second, try

<xsl:for-each select="//*">
<xsl:value-of select="string-join(ancestor-or-self::*/concat(name(), string-join(@*/concat('[@', name(), ']'), '')),/')"/>
</xsl:for-each>


Not tested and almost certainly needs the punctuation adjusting.

Michael Kay
Saxonica

On 09/11/2010 06:44, Ramkumar.V wrote:
Hi, List

How to get complete XPATH from XML file as shown below? I am using xslt2.0with saxon9?

Input XML:
========
<?xml version="1.0" encoding="utf-8"?>
<quizzes>
<quiz id="Q1.1" title="Biochemistry 1.1 Quiz" maxQuestions="4">
<question id="quiz1_1_question1" type="mc">
<prompt>1. Biochemistry is based on the chemistry of which element?</prompt>
<option id="0">Hydrogen</option>
<option id="1">Oxygen</option>
<option id="2" correct = "true">Carbon</option>
<option id="3">Sulfur</option>
<feedback id="0">Incorrect.</feedback>
<feedback id="1">Incorrect.</feedback>
<feedback id="2">Correct!</feedback>
<feedback id="3">Incorrect.</feedback>
</question>
</quiz>
</quizzes>


Expected Output 1(Only Nodes):
=========================
quizzes
quizzes/quiz
quizzes/quiz/question
quizzes/quiz/question/prompt
quizzes/quiz/question/option
quizzes/quiz/question/option
quizzes/quiz/question/option
quizzes/quiz/question/option
quizzes/quiz/question/feedback
quizzes/quiz/question/feedback
quizzes/quiz/question/feedback
quizzes/quiz/question/feedback

Expected Output 2 (With Attributes):
============================
quizzes
quizzes/quiz[@id][@title][@maxQuestions]
quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]
quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/prompt
quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/option[@id]
quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/option[@id]
quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/option[@id][@correct='true']


quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/option[@id]
quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/feedback[@id='0']


quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/feedback[@id='1']

quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/feedback[@id='2']

quizzes/quiz[@id][@title][@maxQuestions]/question[@id][@type]/feedback[@id='3']


XSLT tried:
=========
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:for-each select="descendant-or-self::node()">
<xsl:value-of select="local-name()"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Regards,
Ramkumar V




=========================================================== ***Disclaimer***


This email, and any attachments ("this email"), is confidential. If you are not the addressee please tell the sender immediately, and destroy this email without using, sending or storing it. Any opinions, express or implied, in this email, are those of the sender, and are not necessarily approved by the company. Except as expressly stated, this e-mail should not be regarded as an offer, solicitation, recommendation or agreement to buy or sell products or services, or to enter into any contract. E-mail transmissions are not secure and may suffer errors, viruses, delay, interception and amendment. The company does not accept liability for damage caused by any of the foregoing.


ALL INCOMING AND OUTGOING MAILS MAY BE MONITORED BY THE COMPANY.
============================================================


Current Thread
Keywords