Changing the order of the XML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samueln12
    New Member
    • Feb 2009
    • 28

    Changing the order of the XML

    I want to change the order the caption according to their citation present in the text.

    For example:

    Code:
    <book>
    <chapter>
    <title/>
    <p>1 Some text goes here <xref rid="F1">Figure 1</xref></p>
    <p>2 Some text goes here <xref rid="F2">Figure 2</xref></p>
    <p>3 Some text goes here <xref rid="F4">Figure 4</xref></p>
    <p>3 Some text goes here <xref rid="F3">Figure 3</xref></p>
    <p>4 Some text goes here</p>
    <p>5 Some text goes here <xref rid="F5">Figure 5</xref></p>
    <p>6 Some text goes here <xref rid="F7">Figure 7</xref></p>
    <p>7 Some text goes here</p>
    <p></p>
    <floats>
    <figure id="F1">Figure 1. Figure one text goes here</figure>
    <figure id="F2">Figure 2. Figure one text goes here</figure>
    <figure id="F3">Figure 3. Figure one text goes here</figure>
    <figure id="F4">Figure 4. Figure one text goes here</figure>
    <figure id="F5">Figure 5. Figure one text goes here</figure>
    <figure id="F6">Figure 6. Figure one text goes here</figure>
    <figure id="F7">Figure 7. Figure one text goes here</figure>
    </floats>
    </chapter>
    </book>
    After changing the XML should look like this:
    Code:
    <book>
    <chapter>
    <title/>
    <p>1 Some text goes here <xref rid="F1">Figure 1</xref>
    <figure id="F1">Figure 1. Figure one text goes here</figure>
    </p>
    <p>2 Some text goes here <xref rid="F2">Figure 2</xref>
    <figure id="F2">Figure 2. Figure one text goes here</figure>
    </p>
    <p>3 Some text goes here <xref rid="F4">Figure 4</xref></p>
    <p>3 Some text goes here <xref rid="F3">Figure 3</xref>
    <figure id="F3">Figure 3. Figure one text goes here</figure>
    <figure id="F4">Figure 4. Figure one text goes here</figure>
    </p>
    <p>4 Some text goes here</p>
    <p>5 Some text goes here <xref rid="F5">Figure 5</xref>
    <figure id="F5">Figure 5. Figure one text goes here</figure>
    </p>
    <p>6 Some text goes here <xref rid="F7">Figure 7</xref>
    <figure id="F6">Figure 6. Figure one text goes here</figure>
    <figure id="F7">Figure 7. Figure one text goes here</figure>
    </p>
    <p>7 Some text goes here</p>
    <p></p>
    <floats>
    </floats>
    </chapter>
    </book>
    Please guide me how to do this

    Thanks
    Sam
    Last edited by Dormilich; Apr 12 '10, 11:44 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    which language do you want to use for that?

    Comment

    • samueln12
      New Member
      • Feb 2009
      • 28

      #3
      XSLT version=2.0

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        ?
        Code:
        <xsl:template match="xref">
          <xsl:copy-of select="."/>
          <xsl:copy-of select="descendant::chapter/floats/figure[@id = current/@rid]"/>
        </xsl:template>
        I don't understand how you know where to put
        <figure id="F6">Figure 6. Figure one text goes here</figure>

        Comment

        • samueln12
          New Member
          • Feb 2009
          • 28

          #5
          Originally posted by jkmyoung
          ?
          Code:
          <xsl:template match="xref">
            <xsl:copy-of select="."/>
            <xsl:copy-of select="descendant::chapter/floats/figure[@id = current/@rid]"/>
          </xsl:template>
          I don't understand how you know where to put
          <figure id="F6">Figure 6. Figure one text goes here</figure>
          That is the client requirement. If any citation missing we have to place after the preceding citation.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Could you explain the figure3 and figure4 relation?

            Code:
            <xsl:template match="xref"> 
              <xsl:copy-of select="."/> 
              <xsl:apply-templates select="descendant::chapter/floats/figure[@id = current/@rid]"/> 
            </xsl:template> 
            <xsl:template match="floats"/> <!-- may or may not be necessary depending on your layout-->
            <xsl:template match="figure">
                <xsl:for-each select="preceding-sibling::figure[1]">
                  <xsl:if test="not(//xref[@rid = current()/@id])"><!-- orphan? -->
                     <xsl:apply-templates select="."/>
                  </xsl:if>
                </xsl:for-each>
                <xsl:copy-of select="."/>
            </xsl:template>
            Last edited by jkmyoung; Apr 12 '10, 03:11 PM. Reason: added code

            Comment

            • samueln12
              New Member
              • Feb 2009
              • 28

              #7
              Originally posted by jkmyoung
              Could you explain the figure3 and figure4 relation?
              If citation are not in order e.g. Figure 4 first and Figure 3 next, Figure 4 should placed after figure 3, even though figure 4 citation in first.

              For example, if figure 3 not cited in the text then figure 3 go under figure 2. and Figure 4 place under the Figure 4 paragraph

              I hope this will help u

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                Actually that pretty much completely screws it up.

                In order to get correct order processing, you will have to process using the figures. Processing will have to be done linearly with much checking back, and with state variables, not parallel.

                At this point I would recommend not using XSLT.

                Comment

                • samueln12
                  New Member
                  • Feb 2009
                  • 28

                  #9
                  Originally posted by jkmyoung
                  Actually that pretty much completely screws it up.

                  In order to get correct order processing, you will have to process using the figures. Processing will have to be done linearly, with state variables, not parallel.

                  At this point I would recommend not using XSLT.
                  ok thanks i done this using perl. but i know that using XSLT its very easy.

                  Comment

                  Working...