How to complete this test

M

mike

I am have a problem with my xslt test.

<data>
<struct_id>34</struct_id>
<par_struct_id>0</par_struct_id>
<chd_struct_id>41</chd_struct_id>
</data>
..
..
..

<xsl:for-each select="root/data">
<xsl:choose>
<xsl:when test="par_struct_id = '0'">
<xsl:if test="???">
<xsl:element name="input"> <xsl:attribute
name="type">text</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>

In this example my xml has 3 occurrences where the par_struct_id = '0'
and so I am getting output 3 times so that is cool.

However, within that test I need to write out 1 occurrence of some
element, an input element, but of course I get 3 elements.

How do I limit the input to 1?
 
J

Joris Gillis

Tempore 18:09:01 said:
<xsl:for-each select="root/data">
<xsl:choose>
<xsl:when test="par_struct_id = '0'">
<xsl:if test="???">
<xsl:element name="input"> <xsl:attribute
name="type">text</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>

In this example my xml has 3 occurrences where the par_struct_id = '0'
and so I am getting output 3 times so that is cool.

However, within that test I need to write out 1 occurrence of some
element, an input element, but of course I get 3 elements.

How do I limit the input to 1?

Hi,

If you would like the context node to be the first node (if any) that matches the requirements, then you can use something like:

<xsl:for-each select="root/data[par_struct_id = '0'][1]">
<xsl:element name="input">
<xsl:attribute name="type">text</xsl:attribute>
</xsl:element>
</xsl:for-each>

If the context node does not matter, write 'xsl:if' instead of 'xsl:for-each'.

regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,245
Members
46,839
Latest member
MartinaBur

Latest Threads

Top