Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2016, 02:07 AM
Registered User
 
Join Date: May 2016
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question Parse XML by pattern matching

Hi,
I have an XML file that contains recurring instances of tags like this:
<ABC-question-1>text</ABC-question-1>
<ABC-some-question></ABC-some-question>

tags like the above have similar ABC keyword for all occurrences and these tags donot necessarily apppear together in a particular block in the XML file.

I want to parse these tags with an xslt so as to display all these tags with ABC pattern group together in the output file.

Thnaks in advance.
 
Old May 5th, 2016, 03:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You'll want to use something like "*[starts-with(name(),'ABC')]" to get all the nodes that start with ABC. Other than that its not clear what you want your output to look like.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old May 5th, 2016, 07:46 AM
Registered User
 
Join Date: May 2016
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello samjudson, thanks you for the reply. The output I want is a table to display the tag name in column 1 and tag value in column 2.


Sample XML:

<?xml version="1.0"?>
<name>AayushN</name>
<email>[email protected]</email>
<ABC-question-1>my answer</ABC-question-1>
<ABC-question-2>my answer 2</ABC-question-2>
<dob>00000000</dob>
<ABC-question-3>my answer 3</ABC-question-3>
<alias-name>noName</alias-name>


Output something like:
*****table*********
Column 1 | Column 2
ABC-question-1 | my answer
ABC-question-2 | my answer 2
ABC-question-3 | my answer 3


Output must only contain the ABC prefix tags and it's values only.
 
Old May 5th, 2016, 08:18 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Once you know you want to process the elements with the path that Sam has shown you all you need to do is set up the table and process the elements:
Code:
<xsl:template match="/">
  <html>
     <body>
        <table>
           <thead>
               <tr>
                 <th>Column 1</th>
                 <th>Column 2</th>
               </tr>
            </thead>
            <tbody>
               <xsl:apply-templates select="//*[starts-with(local-name(), 'ABC')]"/>
             </tbody>
         </table>
      </body>
  </html>
</xsl:template>

<xsl:template match="*">
  <tr>
    <td><xsl:value-of select="local-name()"/></td>
    <td><xsl:value-of select="."/></td>
  </tr>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find Matching pattern in Given String sanjivbshinde ASP.NET 2.0 Professional 3 October 10th, 2008 06:32 PM
javascript pattern matching... rbd Javascript 1 October 7th, 2004 12:20 PM
Question on Pattern matching using VBscript Dereksam VBScript 1 September 9th, 2004 04:45 AM
PHP Pattern Matching joanncae BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 May 17th, 2004 04:52 PM
Pattern Matching using PHP spraveens PHP Databases 2 March 23rd, 2004 10:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.