Xpath Help

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

    Xpath Help

    Hi ALL,

    I have a XML file in following type

    Code:
    <xml>
    <para type="P">Sample text 1 goes here</para>
    <para>Sample text 2 goes here</para>
    <para>Sample text 3 goes here</para>
    <para type="P">Sample text 4 goes here</para>
    <para>Sample text 5 goes here</para>
    <para>Sample text 6 goes here</para>
    <para>Sample text 7 goes here</para>
    <para type="P">Sample text 8 goes here</para>
    <para type="P">Sample text 9 goes here</para>
    </xml>
    I want the XML in following type

    Code:
    <xml>
    <para type="P">Sample text 1 goes here</para>
    
    <para type="P">
    <para>Sample text 2 goes here</para>
    <para>Sample text 3 goes here</para>
    Sample text 4 goes here</para>
    
    <para type="P">
    <para>Sample text 5 goes here</para>
    <para>Sample text 6 goes here</para>
    <para>Sample text 7 goes here</para>
    Sample text 8 goes here</para>
    <para type="P">Sample text 9 goes here</para>
    </xml>
    I don't know how to handle this situation. I tried using preceding-sibling and following-sibling.

    Please help me out from this situation

    Thanks
    Sam
    Last edited by Dormilich; Sep 28 '10, 05:54 AM. Reason: please use [code] [/code] tags when posting code
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Modified muenchian grouping:
    Code:
    <xsl:key name="paraP" match="para" use="generate-id((following-sibling::para[@type='P'])[1])"/>
    
    <xsl:template match="xml">
    <copy>
      <xsl:apply-templates match="para[@type='P']"/>
    </copy>
    </xsl:template>
    
    <xsl:template match="para">
      <para type="P">
        <xsl:for-each select="key('paraP', generate-id(.))[not (@type='P')]">
          <xsl:copy-of select="."/>
        </xsl:for-each>
        <xsl:value-of select="."/>
      </para>
    </xsl:template>

    It looks like you're using para[@type="P"] as terminators? New lines might be off a little; modify accordingly.

    Comment

    Working...