0

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>

1 Answer 1

4

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">

<!-- 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.