Home > OS >  XSLT - How to give continuous number followed by text for a particular xml element
XSLT - How to give continuous number followed by text for a particular xml element

Time:11-17

Is there any way to give continuous number followed by text in XSLT. I'm using the below XML File, if the <parent> element has id attribute means I want to get <fo:block> Parent 1 Children of Parent id 100 </block>. Then for second <parent> element, <fo:block> Parent 2 Children of Parent id 180 </block>.

  • Parent [static text] followed by number

XML

<input>
<parent name='Parents' id='100'>
       <Children>Children of Parent id 100</Children>
          <example_child>
             <child name='Child_2' id='2'>example2</child>
             <child name='Child_4' id='4'>example4</child>
          </example_child>
      </parent>
      <parent name='Parents' id='180'>
         <Children>Children of Parent id 180</Children>
            <example_child>
               <child name='Child_1' id='1'>example1</child>
               <child name='Child_3' id='3'>example3</child>
            </example_child>
      </parent>
</input>

XSL

<xsl:template match="parent/Children">   
  <fo:block>      
    <xsl:if test="@id != ''">
            <xsl:value-of select="concat('Parent',' ',continuous number, parent/Children)"/>
    </xsl:if>
  </fo:block>
  <xsl:apply-templates/>
</xsl:template>

Here <xsl:value-of select="concat('Parent',' ', continuous number, parent/Children)"/> 'Parent is static text followed by continuous number then <Children> value. I'm struggling on how to give continuous number on XSL.

Thanks!!

CodePudding user response:

With a well-formed input such as:

XML

<input>
    <parent name="Parents" id="100">
        <Children>Children of Parent id 100</Children>
        <example_child>
            <child name="Child_2" id="2">example2</child>
            <child name="Child_4" id="4">example4</child>
        </example_child>
    </parent>
    <parent name="Parents" id="180">
        <Children>Children of Parent id 180</Children>
        <example_child>
            <child name="Child_1" id="1">example1</child>
            <child name="Child_3" id="3">example3</child>
        </example_child>
    </parent>
</input>

You could do something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template  match="/input">
    <output>
        <xsl:for-each select="parent">
            <block>
                <xsl:text>Parent </xsl:text>
                <xsl:value-of select="position()"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="Children"/>
            </block>
        </xsl:for-each>
    </output>
</xsl:template>
    
</xsl:stylesheet>

to get:

Result

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <block>Parent 1 Children of Parent id 100</block>
   <block>Parent 2 Children of Parent id 180</block>
</output>

which seems close enough to the output you want.

  • Related