Hi Guys,

I am new to XSLT, looking to solve a problem I am facing in filtering XML records.

I need to get both the Input XML record position and limit the output XML to a particular number (based on input provided to xsl:param from .net 1.1)

Input XML

<Parent>
<Child>AAA</Child>
<Child>ABA</Child>
<Child>DAA</Child>
<Child>DBAA</Child>
<Child>BZA</Child>
</Parent>

Output XML(Required)

<table>
<tr>
<td id="Row{Position of the record in Input XML}> node value </td>
</tr>
</table>

Eg: SearchParam='D' and Output Limit=1

if I am searching for one text starting with 'D', the output should be

<table>
<tr>
<td id="Row3>DAA</td>
</tr>
</table>

I am using XSLT:starts-with to get the records I need. But I am only able to limit the count or record position at any one point of time using the position() method.

Can anyone tell me how to achieve this,

Thanks,
Sarath

Recommended Answers

I use param to the outside to control xslt

The first parameter indicates the length of a string
The second parameter is the string being searched

by specifying

and not (preceding-sibling:: Child [substring (.,1,$length) = $alpha]

only the first matching child node appears


       
Jump to Post

All 2 Replies

I use param to the outside to control xslt

The first parameter indicates the length of a string
The second parameter is the string being searched

by specifying

and not (preceding-sibling:: Child [substring (.,1,$length) = $alpha]

only the first matching child node appears

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="html"/>
	<xsl:param name="length" select="1"/>
	<xsl:param name="alpha" select="'B'"/>
	<xsl:template match="/">
		<html>
			<body>
				<xsl:apply-templates select="Parent"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="Parent">
		<table>
			<xsl:apply-templates select="Child"/>
		</table>
	</xsl:template>

	<xsl:template match="Child">
		<xsl:if test="(substring(.,1,$length) = $alpha) and not(preceding-sibling::Child[substring(.,1,$length) = $alpha])">
			<tr>
				<td id="Row{position()}">
					<xsl:value-of select="."/>
				</td>
			</tr>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

result param1=1 param2='B'

<html>
  <body>
    <table>
      <tr>
        <td id="Row5">BZA</td>
      </tr>
    </table>
  </body>
</html>

hello...
my name is Maruli Situmorang
I want to ask the all master of java
how transform pdf file to xml in java
thank a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.