tags:

views:

89

answers:

1

Hello,

How I can transform a xml like this:

<xml>
   <paragraph>
        <data>
            <label> title </label>
         </data>
         <data>
            <label> subtitle </label>
         </data>

         <data>
            <label> text </label>
         </data>

         <data>
            <label> image </label>
         </data>
   </paragraph>
</xml>

In the following xml (image before the text):

<xml>
   <paragraph>
        <data>
            <label> title </label>
         </data>
         <data>
            <label> subtitle </label>
         </data>
         <data>
            <label> image </label>
         </data>
         <data>
            <label> text </label>
         </data>
   </paragraph>
</xml>

I really need to call the template of the images rather than the text if next node is an image. (The xml source is messy in that field)

Then I copy some code I'm using xslt:

                  <xsl:when test="etiqueta = 'Imagen'">
                    <xsl:call-template name="imprimeImagenParrafo">
                      <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                      <xsl:with-param name="valor" select="valor"></xsl:with-param>
                      <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                      <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                      <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                      <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                    </xsl:call-template>
                  </xsl:when>
                </xsl:when>
+2  A: 

You'll have to adjust this to match your data (I'm assuming the sample source document isn't really what you have), but perhaps it will inspire you:

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<!-- Identity copy that just passes through everything -->
<xsl:template match="@*|node()">
  <xsl:apply-templates select="@*|node()"/>
</xsl:template>

<!-- Here we match the paragraph element, copy it and its attrs, then rearrange it's children -->
<xsl:template match="paragraph">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <!-- Edit the predicates as necessary for your real data -->
    <!-- predicates are the things in the []...like the WHERE in a SQL query -->
    <xsl:apply-templates select="data[normalize-space(label) = 'title']"/>
    <xsl:apply-templates select="data[normalize-space(label) = 'subtitle']"/>
    <xsl:apply-templates select="data[normalize-space(label) = 'image']"/>
    <xsl:apply-templates select="data[normalize-space(label) = 'text']"/>
  </xsl:copy>
</xsl:template>

Good luck, David

David