User014019
User014019

Reputation: 1247

What is the right code to hide fields in XML using XSLT?

How to hide the other fields if the option in spinner is not selected using XML and XSLT?

E.g I have a spinner with three options

Option 1: Form 1
Option 2: Form 2
Option 3: Others

If the user choose option 1, all fields under option 1 will display, while the fields under option 2 is hide (vise versa)

Is this correct?

 <xsl:template match="/">
    <xsl:apply-templates select="Form/Field">   
    <xsl:variable name="Category" select="Spinner"/>
        <select id="Form">
            <xsl:for-each select="$Item">
                <xsl: value="{.}">
                    <xsl:attribute name="type"/>
                </xsl:if>
                <xsl:value-of select="."/>
</xsl:template>

XML:

<Field name="Category" type="Spinner" label="Please choose">
    <Item code="CashPickupForm" label="Cash Pickup"/>
    <Item code="HomeVisitForm" label="Home Visit"/>
    <Item code="OtherForm" label="Others"/>
</Field>

Here's some XML of Form 1

<Form name="CashPickup"
  type="TextView"
  label="Cash Pick-up Form">

<Field name="ContractNumber" type="TextView" label="Contract number" value=""/>
<Field name="ClientName" type="TextView" label="Client Name" value=""/>
<Field type="Delimiter"/>

XSL:

  <xsl:template match="/">
<xsl:apply-templates select="Form"/>
</xsl:template>

<xsl:template match="Form">
<xsl:element name="Form">
  <xsl:attribute name="name">
    <xsl:value-of select="@name"/>
  </xsl:attribute>
  <xsl:attribute name="type">
    <xsl:value-of select="@type"/>
  </xsl:attribute>
  <xsl:attribute name="label">
    <xsl:value-of select="@label"/>
  </xsl:attribute>
  <xsl:copy-of select="namespace::*[not(name()='ns2') and not(name()='')]"/>
  <xsl:call-template  name="Arrange"/>
</xsl:element>
</xsl:template>

<xsl:template name="Arrange">

<xsl:apply-templates select="Field[@name='ContractNumber']"/>
<xsl:apply-templates select="Field[@name='ClientName']"/>
<Field type="Delimiter"/>

XML Form 2:

<Form name="HomeVisitForm"
  type="TextView"
  label="Home Visit Form">

<Field name="ContractNumber" type="TextView" label="Application number" value=""/>
<Field name="TypeCheck" type="TextView" label="Type of check" value=""/>
<Field type="Delimiter"/>

XSL:

<xsl:template match="/">
<xsl:apply-templates select="Form"/>
</xsl:template>

<xsl:template match="Form">
<xsl:element name="Form">
  <xsl:attribute name="name">
    <xsl:value-of select="@name"/>
  </xsl:attribute>
  <xsl:attribute name="type">
    <xsl:value-of select="@type"/>
  </xsl:attribute>
  <xsl:attribute name="label">
    <xsl:value-of select="@label"/>
  </xsl:attribute>
  <xsl:copy-of select="namespace::*[not(name()='ns2') and not(name()='')]"/>
  <xsl:call-template  name="Arrange"/>
 </xsl:element>
</xsl:template>

<xsl:template name="Arrange">

<xsl:apply-templates select="Field[@name='ContractNumber']"/>
<xsl:apply-templates select="Field[@name='TypeCheck']"/>
<Field type="Delimiter"/>

Upvotes: 1

Views: 5044

Answers (1)

Marcus Rickert
Marcus Rickert

Reputation: 4238

Suppose you pass a parameter called FormType to your XSLT containing either the value 1 or 2 (as a start and possibly other values later on) you could enhance your XSLT in the following way:

...
<xsl:parameter name="FormType"/>
...
<xsl:template match="/">
  <xsl:apply-templates select="Form"/>
</xsl:template>

<xsl:template match="Form">
  <Form name="{@name}" 
        type="{@type}"
        label="{@label}">
    <xsl:copy-of select="namespace::*[not(name()='ns2') and not(name()='')]"/>
    <xsl:call-template name="Arrange"/>
  </Form>
</xsl:template>

<xsl:template name="Arrange">

  <xsl:apply-templates select="Field[@name='ContractNumber']"/>

  <xsl:if test="$FormType = '1'">
    <xsl:apply-templates select="Field[@name='ClientName']"/>
    <!-- you may put more fields here if applicable-->
  </xsl:if>

  <xsl:if test="$FormType = '2'">
    <xsl:apply-templates select="Field[@name='TypeCheck']"/>
  </xsl:if>
  ...
  <!-- you may put more if blocks here if applicable -->
  ...
  <Field type="Delimiter"/>
  ...
</xsl:template>

Notes:

  • I have simplified your <xsl:element> tags using simple HTML tags.
  • This is just an example for one field. You may have to add more <xsl:if> tags if required.
  • If the order is right you may put more than one field into one <xsl:if> block.

Upvotes: 1

Related Questions