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 February 13th, 2009, 03:25 PM
Authorized User
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dved Send a message via Yahoo to dved
Default Returning PSV having headers once in whole parsing

Hi,

I am applying XSLT to multiple xmls. I want that XSLT returns names of the tag only once(pipe separated) with data values. for Eg.

<xsl:template match="root">
<xsl:for-each select="//*">
<xsl:value-of select="local-name()"/>|<xsl:value-of select="current()/text()"/>|
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>

The above code returns me the names of the tag with values but i want names of tag only once.

so if I have an XML like :
<x>
<a>1</a>
<b>2</b>
<c>3</c>
</x>
<x>
<a>4</a>
<b>5</b>
<c>6</c>
</x>
The output should be like :
a|b|c|1|2|3|4|5|6|

Please help
__________________
Thanks
Dved
 
Old February 16th, 2009, 03:04 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try this:

Code:
 
<xsl:template match="/">
<xsl:variable name="child">
<xsl:value-of select="distinct-values(//*[. = text()]/local-name())"></xsl:value-of>
</xsl:variable>
<xsl:value-of select="translate($child, ' ', '|')"></xsl:value-of>
<xsl:for-each select="//*[. = text()]">
<xsl:if test="position() = 1">
<xsl:text>|</xsl:text>
</xsl:if>
<xsl:value-of select="."></xsl:value-of>
<xsl:if test="position() != last()">
<xsl:text>|</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
__________________
Rummy
 
Old February 16th, 2009, 06:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your example output suggests that you actual requirement is something like "Show the names and values of all the elements that have no element children, eliminating duplicates". In XSLT 2.0 that is

<xsl:template match="/">
<xsl:value-of select="distinct-values(//*[not(*)]/(name(.), string(.)))" separator="|"/>
</xsl:template>


In XSLT 1.0 it's going to be MUCH more complicated.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 18th, 2009, 11:36 AM
Authorized User
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dved Send a message via Yahoo to dved
Exclamation did not get it

Well what I m trying to do is I want to extract the names of the tag only once in whole parsing so that I can treat them as column header while creating my CSV.

this is what I m trying to write but it does not work:

<xsl:template match="paymentHistoryList" >
<xsl:apply-templates ></xsl:apply-templates>
</xsl:template>
<!--This part is what I m interested in..--!>
<xsl:template match="paymentHistoryInfo" >
<xsl:for-each select="*/paymentHistoryList/*">
<xsl:value-of select="local-name()"/>
<xsl:value-of select="'|'"/>
</xsl:for-each>
&NL;

</xsl:template>

<!--This works well --!>
<xsl:template match="paymentHistoryInfo">

<xsl:for-each select="*">
<xsl:value-of select="."/>
<xsl:choose>
<xsl:when test="position() != last()">
<xsl:value-of select="'|'"/>
</xsl:when>
<xsl:when test="position() = last()">
&NL;
</xsl:when>
</xsl:choose>


</xsl:for-each>

</xsl:template>

MY XML is like this:

<serviceHeader>
<billingSys>VISION_EAST</billingSys>
<clientId>VZW-FIN</clientId>
<userId>00000003</userId>
<password>01VISION</password>
<serviceName>PaymentHistoryInquiry</serviceName>
<statusCode>00</statusCode>
<errorCode>00</errorCode>
<errorMsg/>
<cicsErrorCode>00</cicsErrorCode>
<serverName>vmwdev2</serverName>
<correlationId>c48e6a2ad3c47cc5:-2cb74c38:11f810f0494:-7a85:vmwdev2:DVSCOMMON</correlationId>
</serviceHeader>
<serviceBody>
<serviceRequest>
<accountNo>15241-1</accountNo>
</serviceRequest>
<serviceResponse>
<paymentHistoryRowCount>100</paymentHistoryRowCount>
<paymentHistoryList>
<paymentHistoryInfo>
<invoiceDate>200812</invoiceDate>
<invoiceNumber>1493001562</invoiceNumber>
<previousBalance>84.91</previousBalance>
<paymentsRecieved>84.91</paymentsRecieved>
<balanceForward>0.0</balanceForward>
<currentCharges>83.96</currentCharges>
<totalBalance>83.96</totalBalance>
</paymentHistoryInfo>
<paymentHistoryInfo>
<invoiceDate>200811</invoiceDate>
<invoiceNumber>1482401248</invoiceNumber>
<previousBalance>173.66</previousBalance>
<paymentsRecieved>173.66</paymentsRecieved>
<balanceForward>0.0</balanceForward>
<currentCharges>84.91</currentCharges>
<totalBalance>84.91</totalBalance>
</paymentHistoryInfo>
</paymentHistoryList>
</serviceResponse>
</serviceBody>
</service>
__________________
Thanks
Dved
 
Old February 18th, 2009, 01:26 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 1.0 stylesheet that outputs the names of child elements in a header line, then the values in following lines:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output method="text"/>
  
  <xsl:param name="nl" select="'
'"/>
  <xsl:param name="sep" select="'|'"/>
  
  <xsl:template match="/">
    <xsl:apply-templates select="service/serviceBody/serviceResponse/paymentHistoryList/paymentHistoryInfo[1]/*" mode="header"/>
    <xsl:apply-templates select="service/serviceBody/serviceResponse/paymentHistoryList/paymentHistoryInfo"/>
  </xsl:template>
  
  <xsl:template match="paymentHistoryInfo/*" mode="header">
    <xsl:value-of select="local-name()"/>
    <xsl:choose>
      <xsl:when test="position() != last()">
        <xsl:value-of select="$sep"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$nl"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="paymentHistoryInfo">
    <xsl:apply-templates select="*"/>
  </xsl:template>

  <xsl:template match="paymentHistoryInfo/*" >
    <xsl:value-of select="."/>
    <xsl:choose>
      <xsl:when test="position() != last()">
        <xsl:value-of select="$sep"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$nl"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
When applied to the input document
Code:
<?xml version="1.0" encoding="utf-8" ?>
<service>
  <serviceHeader>
    <billingSys>VISION_EAST</billingSys>
    <clientId>VZW-FIN</clientId>
    <userId>00000003</userId>
    <password>01VISION</password>
    <serviceName>PaymentHistoryInquiry</serviceName>
    <statusCode>00</statusCode>
    <errorCode>00</errorCode>
    <errorMsg/>
    <cicsErrorCode>00</cicsErrorCode>
    <serverName>vmwdev2</serverName>
    <correlationId>c48e6a2ad3c47cc5:-2cb74c38:11f810f0494:-7a85:vmwdev2:DVSCOMMON</correlationId>
  </serviceHeader>
  <serviceBody>
    <serviceRequest>
      <accountNo>15241-1</accountNo>
    </serviceRequest>
    <serviceResponse>
      <paymentHistoryRowCount>100</paymentHistoryRowCount>
      <paymentHistoryList>
        <paymentHistoryInfo>
          <invoiceDate>200812</invoiceDate>
          <invoiceNumber>1493001562</invoiceNumber>
          <previousBalance>84.91</previousBalance>
          <paymentsRecieved>84.91</paymentsRecieved>
          <balanceForward>0.0</balanceForward>
          <currentCharges>83.96</currentCharges>
          <totalBalance>83.96</totalBalance>
        </paymentHistoryInfo>
        <paymentHistoryInfo>
          <invoiceDate>200811</invoiceDate>
          <invoiceNumber>1482401248</invoiceNumber>
          <previousBalance>173.66</previousBalance>
          <paymentsRecieved>173.66</paymentsRecieved>
          <balanceForward>0.0</balanceForward>
          <currentCharges>84.91</currentCharges>
          <totalBalance>84.91</totalBalance>
        </paymentHistoryInfo>
      </paymentHistoryList>
    </serviceResponse>
  </serviceBody>
</service>
the output is as follows:
Code:
invoiceDate|invoiceNumber|previousBalance|paymentsRecieved|balanceForward|currentCharges|totalBalance
200812|1493001562|84.91|84.91|0.0|83.96|83.96
200811|1482401248|173.66|173.66|0.0|84.91|84.91
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 19th, 2009, 03:11 PM
Authorized User
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dved Send a message via Yahoo to dved
Thumbs up

Thanks for that..got it..
__________________
Thanks
Dved





Similar Threads
Thread Thread Starter Forum Replies Last Post
jpgraph+headers tessy Beginning PHP 0 May 7th, 2008 12:03 AM
Hiding sub headers.... rupen Crystal Reports 6 May 14th, 2007 04:56 AM
CheckBoxList Headers Ric_H C# 0 June 26th, 2006 10:29 AM
"headers already sent" - one cure tks001 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 February 20th, 2006 07:05 PM
Headers skicrud Beginning PHP 2 October 10th, 2003 02:21 PM





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