Hello
I would like help in creating an xsl (and to flatten out the learning curve). Im new to using xsl, but have good experience with dom trees and xml. Using xsl looks like it can do many things I have been coding manually. What I need now is to take a xml doc and remove all attributes and empty tags. (I consider empty tags as tag without any data). To help illustrate this, below is a sample of the input/source xml and the resulting xml.
xml source
<Sales_ticket>
<Sales_ticket_header>
<Sales_ticket_header_Trans
mit_versio
n/>
<Sales_ticket_header_Store
_number/>
<Sales_ticket_header_Regis
ter_ID/>
<Sales_ticket_header_Trans
action_dat
e/>
<Sales_ticket_header_Trans
action_tim
e/>
<Sales_ticket_header_Polle
d_date ref="XDT50">11232005</Sale
s_ticket_h
eader_Poll
ed_date>
<Sales_ticket_header_Ticke
t_number ref="X50TICK">36154</Sales
_ticket_he
ader_Ticke
t_number>
<Sales_ticket_header_Recep
tionist_ID
ref="X50RECP">00</Sales_ti
cket_heade
r_Receptio
nist_ID>
<Sales_ticket_header_Emplo
yee_ID ref="X50EMPN">05</Sales_ti
cket_heade
r_Employee
_ID>
<Sales_ticket_header_Custo
mer_type ref="X50CTYP">4</Sales_tic
ket_header
_Customer_
type>
<Sales_ticket_header_price
_group ref="X50PGRP">020</Sales_t
icket_head
er_price_g
roup>
<Sales_ticket_header_POS_s
ales_tax ref="X50STAX">000000.00</S
ales_ticke
t_header_P
OS_sales_t
ax>
</Sales_ticket_header>
<Sales_ticket_detail>
<Sales_ticket_detail_Ticke
t_number ref="X51TICK">36154</Sales
_ticket_de
tail_Ticke
t_number>
<Sales_ticket_detail_Emplo
yee_ID ref="X51EMPN">05</Sales_ti
cket_detai
l_Employee
_ID>
<Sales_ticket_detail_Detai
l_price_gr
oup ref="X51PGRP">020</Sales_t
icket_deta
il_Detail_
price_grou
p>
<Sales_ticket_detail_Retai
l_or_servi
ce_flag ref="X51TYPE">S</Sales_tic
ket_detail
_Retail_or
_service_f
lag>
<Sales_ticket_detail_Retai
l_or_servi
ce_number ref="X51COD1">00865</Sales
_ticket_de
tail_Retai
l_or_servi
ce_number>
<Sales_ticket_detail_Subse
rvice_numb
er ref="X51COD2">000</Sales_t
icket_deta
il_Subserv
ice_number
>
<Sales_ticket_detail_List_
price ref="X51LPRC">000012.50</S
ales_ticke
t_detail_L
ist_price>
<Sales_ticket_detail_Quant
ity_sold ref="X51QTYS">01</Sales_ti
cket_detai
l_Quantity
_sold>
<Sales_ticket_detail_Item_
taxed ref="NG51ITAX">N</Sales_ti
cket_detai
l_Item_tax
ed>
</Sales_ticket_detail>
<Payment_detail>
<Payment_detail_Ticket_num
ber ref="X54TICK">36154</Payme
nt_detail_
Ticket_num
ber>
<Payment_detail_Payment_ty
pe ref="X54TYPE">1</Payment_d
etail_Paym
ent_type>
<Payment_detail_Amount_ten
dered ref="X54AMNT">000000000005
0.00</Paym
ent_detail
_Amount_te
ndered>
<Payment_detail_Amount_neg
ative ref="NG54NEGA">N</Payment_
detail_Amo
unt_negati
ve>
<Payment_detail_Gift_certi
ficate_num
ber_redeem
ed ref="X54GCRD">000000000</P
ayment_det
ail_Gift_c
ertificate
_number_re
deemed>
<Payment_detail_Change_rec
ord ref="NG54CHNG">N</Payment_
detail_Cha
nge_record
>
<Payment_detail_Change_amo
unt ref="NG54CAMT">00000000000
00.00</Pay
ment_detai
l_Change_a
mount>
<Credit_card_detail>
<Credit_card_detail_Ticket
_number ref="NG55TICK"/>
<Credit_card_detail_Charge
_amount ref="NG55CHGA"/>
</Credit_card_detail>
</Payment_detail>
</Sales_ticket>
desired xml result
<Sales_ticket>
<Sales_ticket_header>
<Sales_ticket_header_Polle
d_date>112
32005</Sal
es_ticket_
header_Pol
led_date>
<Sales_ticket_header_Ticke
t_number>3
6154</Sale
s_ticket_h
eader_Tick
et_number>
<Sales_ticket_header_Emplo
yee_ID>05<
/Sales_tic
ket_header
_Employee_
ID>
<Sales_ticket_header_Custo
mer_type>4
</Sales_ti
cket_heade
r_Customer
_type>
<Sales_ticket_header_price
_group>020
</Sales_ti
cket_heade
r_price_gr
oup>
<Sales_ticket_header_POS_s
ales_tax>0
00000.00</
Sales_tick
et_header_
POS_sales_
tax>
</Sales_ticket_header>
<Sales_ticket_detail>
<Sales_ticket_detail_Ticke
t_number>3
6154</Sale
s_ticket_d
etail_Tick
et_number>
<Sales_ticket_detail_Emplo
yee_ID>05<
/Sales_tic
ket_detail
_Employee_
ID>
<Sales_ticket_detail_Detai
l_price_gr
oup>020</S
ales_ticke
t_detail_D
etail_pric
e_group>
<Sales_ticket_detail_servi
ce_flag>S<
/Sales_tic
ket_detail
_service_f
lag>
<Sales_ticket_detail_servi
ce_number>
00865</Sal
es_ticket_
detail_ser
vice_numbe
r>
<Sales_ticket_detail_Subse
rvice_numb
er>000</Sa
les_ticket
_detail_Su
bservice_n
umber>
<Sales_ticket_detail_List_
price>0000
12.50</Sal
es_ticket_
detail_Lis
t_price>
<Sales_ticket_detail_Quant
ity_sold>0
1</Sales_t
icket_deta
il_Quantit
y_sold>
<Sales_ticket_detail_Item_
taxed>N</S
ales_ticke
t_detail_I
tem_taxed>
</Sales_ticket_detail>
<Payment_detail>
<Payment_detail_Ticket_num
ber>36154<
/Payment_d
etail_Tick
et_number>
<Payment_detail_Payment_ty
pe>1</Paym
ent_detail
_Payment_t
ype>
<Payment_detail_Amount_ten
dered>0000
00050.00</
Payment_de
tail_Amoun
t_tendered
>
<Payment_detail_Amount_neg
ative>N</P
ayment_det
ail_Amount
_negative>
<Payment_detail_Gift_certi
ficate_num
ber_redeem
ed>000</Pa
yment_deta
il_Gift_ce
rtificate_
number_red
eemed>
<Payment_detail_Change_rec
ord>N</Pay
ment_detai
l_Change_r
ecord>
<Payment_detail_Change_amo
unt>000000
00.00</Pay
ment_detai
l_Change_a
mount>
</Payment_detail>
</Sales_ticket>
Basically Im trying to reduce the document size before sending it on its way.
Here is what I got so far, it seems to just remove the attributes, but not the empty tags. Not sure what tells it to remove the attributes either. So 500 points for one that does both, with comments telling what part does what. Thanks.
<xsl:stylesheet xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/
>
<xsl:template match="* | /">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Start Free Trial