Help with transforming XML document

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mirjana Rakuljic

    #1

    Help with transforming XML document

    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_r ow num="1">
    <apellido>Riu s</apellido>
    <nombre>Jaume </nombre>
    <naf null="YES"/>
    <antiguedad>
    <antiguedad_r ow num="1">
    <meses>15</meses>
    <dias>0</dias>
    </antiguedad_row>
    </antiguedad>
    <atep null="YES"/>
    <domicilio>CL.B arcelona, 27</domicilio>
    <telefono null="YES"/>
    </trabajador_row>
    </trabajador>
    </PAT>
    </MultiPAT>

    The problem is that the tags like <trabajador_r ow 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>Riu s</apellido>
    <nombre>Jaume </nombre>
    <naf/>
    <antiguedad>
    <meses>15</meses>
    <dias>0</dias>
    </antiguedad>
    <atep/>
    <domicilio>CL.B arcelona, 27</domicilio>
    <telefono/>
    </trabajador>
    </PAT>
    </MultiPAT>

    Any ideas how to do transformation?

    Thanks!
  • Patrick TJ McPhee

    #2
    Re: Help with transforming XML document

    In article <a667350c.03110 62343.1a0da45c@ posting.google. com>,
    Mirjana Rakuljic <mime49@yahoo.c om> wrote:

    % I need help with transforming XML document.

    [very clear description of problem elided]

    % <trabajador>
    % <trabajador_r ow num="1">

    [...]

    % The problem is that the tags like <trabajador_r ow 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:transfor m 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:transfor m match="trabajad or/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:transfor m 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
    ptjm@interlog.c om

    Comment

    • Mirjana Rakuljic

      #3
      Re: Help with transforming XML document

      Thank you very much for your help!!!!

      Comment

      Working...