【问题标题】:What is the right code to hide fields in XML using XSLT?使用 XSLT 隐藏 XML 中的字段的正确代码是什么?
【发布时间】:2014-10-10 09:24:02
【问题描述】:

如果使用 XML 和 XSLT 未选择微调器中的选项,如何隐藏其他字段?

例如,我有一个带有三个选项的微调器

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>

这是表格 1 的一些 XML

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

【问题讨论】:

  • 您找到解决方案了吗?如果是这样,请删除该问题,因为它在当前状态下有点不稳定。如果没有,请发表评论,我们可以尝试为您的问题添加大量信息,以供回答。
  • 还没有。我仍在为这个@MarcusRickert 寻找解决方案
  • 好的。所以这些是我最初的问题/建议:在提供 XML 文件时,请确保您声明为 Source XML(您的输入)或 Target XML(所需的输出)。您应该只有一个 XSLT(目前您有两个 sn-ps)。您的 XML 文件有不平衡的标签。你能仔细检查一下吗?要使 XSLT 对当前选择做出反应,您必须将此选择存储在变量中的某个位置。将此选择传递给 XSLT 的首选方式是什么?参数?
  • @MarcusRickert 是的,尽可能通过参数
  • 我的其他问题/cmets呢?

标签: xml xslt xml-parsing show-hide


【解决方案1】:

假设您将一个名为 FormType 的参数传递给您的 XSLT,其中包含值 12(作为开始,稍后可能还有其他值),您可以通过以下方式增强您的 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 标记简化了您的 &lt;xsl:element&gt; 标记。
  • 这只是一个字段的示例。如果需要,您可能需要添加更多 &lt;xsl:if&gt; 标签。
  • 如果顺序正确,您可以将多个字段放入一个 &lt;xsl:if&gt; 块中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2011-11-02
    • 2012-03-31
    • 1970-01-01
    • 2018-04-26
    • 2014-04-10
    • 2011-02-22
    相关资源
    最近更新 更多