Help with transforming XML document

  • Thread starter Mirjana Rakuljic
  • Start date
M

Mirjana Rakuljic

Hi,
I need help with transforming XML document.

I generate xml document from query using Oracle XSU (DBMS_XMLQUERY)
and the document I get is like this:

<?xml version = '1.0' encoding = 'ISO-8859-1'?>
<MultiPAT>
<PAT>
<numreferencia>000000000000</numreferencia>
<tipo>1</tipo>
<trabajador>
<trabajador_row num="1">
<apellido>Rius</apellido>
<nombre>Jaume</nombre>
<naf null="YES"/>
<antiguedad>
<antiguedad_row num="1">
<meses>15</meses>
<dias>0</dias>
</antiguedad_row>
</antiguedad>
<atep null="YES"/>
<domicilio>CL.Barcelona, 27</domicilio>
<telefono null="YES"/>
</trabajador_row>
</trabajador>
</PAT>
</MultiPAT>

The problem is that the tags like <trabajador_row num="1"> are
redundant and I'd also like to eliminate attributes null="YES" (that
can be found within empty tags).
I'd like to get something like this:

<?xml version = '1.0' encoding = 'ISO-8859-1'?>
<MultiPAT>
<PAT>
<numreferencia>000000000000</numreferencia>
<tipo>1</tipo>
<trabajador>
<apellido>Rius</apellido>
<nombre>Jaume</nombre>
<naf/>
<antiguedad>
<meses>15</meses>
<dias>0</dias>
</antiguedad>
<atep/>
<domicilio>CL.Barcelona, 27</domicilio>
<telefono/>
</trabajador>
</PAT>
</MultiPAT>

Any ideas how to do transformation?

Thanks!
 
P

Patrick TJ McPhee

% I need help with transforming XML document.

[very clear description of problem elided]

% <trabajador>
% <trabajador_row num="1">

[...]

% The problem is that the tags like <trabajador_row num="1"> are
% redundant and I'd also like to eliminate attributes null="YES" (that
% can be found within empty tags).

[...]

% Any ideas how to do transformation?

Build the transformation around the so-called `identity transform'.

<xsl:transform match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<xsl:copy>
</xsl:transform>

This matches every node and every attribute node. The attribute nodes
have to be specified separately because the attribute axis isn't
searched by default.

You then specify transformations for the specific nodes you want to
handle specially. Because of the nature of the pattern match, the
identity transform is assigned a lower priority than a more specific
transform.

<xsl:transform match="trabajador/trabajador_row">
<xsl:apply-templates select="node()"/>
</xsl:transform>

That will copy the non-attribute children of trabajador_row, without
copying the trabajador_row element. I leave out the attributes because
you don't seem to like them, but you could put more detailed selection
criteria in place if you need more specific selection criteria on the
attribute side. If you have a lot of data and prepared to forget about
your null='yes' requirement, you could replace `apply-templates'
with `copy-of', which is likely to be a bit faster.

It's worth learning about xpath, incidentally. For one thing, you
really need to know about it if you want to use xslt, and for another,
it's fairly well-designed, and the spec is relatively well-written, so
you don't have to invest too much effort on it.

To get rid of the null="yes" attributes, you could do this:

<xsl:transform match="*[@null='YES']">
<xsl:copy/>
</xsl:transform>

since they're always empty, there's no need to apply any further templates.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top