2014-10-10 59 views
1

如果未使用XML和XSLT选择微调器中的选项,如何隐藏其他字段?使用XSLT在XML中隐藏字段的正确代码是什么?

例如,我有三个选项

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

一个微调如果用户选择选项1,选项1下的所有字段将显示,同时在方案2中的字段是隐藏的(反之亦然)

它是否正确?

<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> 

这里的表的一些XML 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表格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"/> 
+0

您是否找到了解决方案?如果是这样,请删除该问题,因为它在当前状态中有点不稳定。如果不是,留下评论,我们可以尝试添加实质性信息到您的问题,这将需要回答它。 – 2014-10-12 12:57:59

+0

还没有。我仍然在为此寻找解决方案@MarcusRickert – User014019 2014-10-12 13:00:08

+0

好。因此,这些是我最初的问题/建议:提供XML文件时,请确保声明为*源XML *(您的输入)或*目标XML *(所需输出)。你应该只有一个XSLT(目前你有两个片段)。您的XML文件具有不平衡的标签。你可以仔细检查一下吗?为了使XSLT对当前选择做出反应,你必须将这个选择存储在一个变量中。将此选择传递给XSLT的首选方式是什么?一个参数? – 2014-10-12 13:09:40

回答

1

假设你通过一个名为FormType包含任何价值12您的XSLT参数(如开始和其他可能的值以后),你可以提高你的XSLT通过以下方式:

... 
<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> 

注:

  • 我使用简单的HTML标签简化了您的<xsl:element>标签。
  • 这只是一个领域的例子。如有需要,您可能需要添加更多<xsl:if>标签。
  • 如果订单是正确的,您可以将一个以上的字段放入一个<xsl:if>区块。