Help with transforming XML document

Discussion in 'XML' started by Mirjana Rakuljic, Nov 7, 2003.

  1. 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!
    Mirjana Rakuljic, Nov 7, 2003
    #1
    1. Advertising

  2. In article <>,
    Mirjana Rakuljic <> wrote:

    % 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.
    --

    Patrick TJ McPhee
    East York Canada
    Patrick TJ McPhee, Nov 7, 2003
    #2
    1. Advertising

  3. Thank you very much for your help!!!!
    Mirjana Rakuljic, Nov 8, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Furey
    Replies:
    0
    Views:
    1,073
    David Furey
    Jun 29, 2003
  2. Tor Hovland
    Replies:
    0
    Views:
    376
    Tor Hovland
    Sep 15, 2003
  3. Tony Prichard
    Replies:
    0
    Views:
    706
    Tony Prichard
    Dec 12, 2003
  4. Manish Hatwalne
    Replies:
    1
    Views:
    387
    Martin Honnen
    Jul 13, 2004
  5. samppi
    Replies:
    1
    Views:
    404
    Martin Honnen
    Jan 6, 2008
Loading...

Share This Page