【问题标题】:How to use choose like if else condition in xslt [closed]如何在 xslt 中使用 select like if else 条件 [关闭]
【发布时间】:2016-04-19 22:30:05
【问题描述】:

嗨,任何了解 xslt 的人都可以帮助我。 我只想显示复选框,如果有值复选框必须选中,否则不选中。我使用了选择,但它没有给我想要的东西。它只给了我一个选中的复选框。

这是我的 C# 代码

  public DataSet printIST(long Id)
    {
        var query0 = (from request in _entities.ISTMotivations
                        select new
                        {
                            ISTID = request.ISTID,
                            ISTMotivation = request.ISTReason.LookupDesc,
                            ISTMotivationID = request.ISTMotivationID,
                            ISTReasonID = request.ISTReasonID
                        }).OrderBy(x => x.ISTReasonID);
        var query2 = (from request in _entities.ISTMotivations
                        where request.ISTID==Id
                        select new
                        {
                            ISTMotivationID=request.ISTMotivationID,
                            ISTID=request.ISTID,
                            ISTReasonID=request.ISTReasonID
                        }).OrderBy(x=>x.ISTReasonID);
        DataSet ds = new DataSet();
        ds.Tables.Add(query0.CopyToDataTable()); ds.Tables[0].TableName = "Table4";
        ds.Tables.Add(query2.CopyToDataTable()); ds.Tables[1].TableName = "Table5";
        return ds;
    }

这是我的 XSLT 代码

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
      <xsl:output method="xml" indent="yes"/>
      <xsl:key name ="keyMoticationID" match ="NewDataSet/Table4" use ="ISTMotivationID"/>
      <xsl:key name ="keycheck" match ="NewDataSet/Table5" use ="concat(ISTID, '+',ISTMotivationID)"/>
      <xsl:template match="/">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
          <html>
            <xsl:variable name="lst4" select="//NewDataSet/Table4" />
            <xsl:variable name="lst5" select="//NewDataSet/Table5" />
            <style>
              TABLE {empty-cells: show; border-spacing: 0px; margin: 0px; padding: 0px;width:100%;}
              .pagebreak {page-break-after: always;}
              TD.HeaderText {font-family:Arial;font-size:14pt;border:0;margin:0;background:none;font-weight:bold;}
              TR.HeaderText {font-family:Arial;font-size:14pt;border:0;margin:0;background:none;font-weight:bold;}
              TR.NormalText {font-family:Arial;font-size:10pt;border:0;margin:0;background:none;font-weight:normal;}
              TD.NormalText {font-family:Arial;font-size:10pt;border:0;margin:0;background:none;font-weight:normal;}
              TABLE.SpacedRows TD {padding:2pt 0 8pt 0; vertical-align:top;}
              TABLE.NormalText {font-family:Arial;font-size:10pt;border:0;margin:0;background:nonkeyIDe;font-weight:normal;}
            </style>
            <title>
              <center>Application for IST transfer</center>
            </title>
    <table border="1" cellspacing="0" cellpadding="0"   width="100%" style="border-style:solid;border-color:Black;border-width:1px;font-family:Arial;border-collapse: collapse;font-size:11px;" >
      <xsl:for-each select="$lst4[generate-id(.) = generate-id(key('keyMoticationID', ISTMotivationID)[1])]">
        <xsl:variable name="intISTMotivationID">
          <xsl:value-of select="ISTMotivationID" />
        </xsl:variable>
        <tr>
          <td style="width:2%; padding:2px;text-align:center;">
            <xsl:value-of select ="$lst4[ISTMotivationID=$intISTMotivationID]/ISTReasonID"/>
          </td>
          <td style="width:96%; padding:2px;">
            <xsl:value-of select ="$lst4[ISTMotivationID=$intISTMotivationID]/ISTMotivation"/>
          </td>

        <xsl:for-each select="$lst5[generate-id(.) = generate-id(key('keycheck',concat(ISTID,'+', $intISTMotivationID))[1])]">
        <xsl:variable name="intISTID">
          <xsl:value-of select="ISTID" />
        </xsl:variable>
          <xsl:choose>
            <xsl:when test="$lst5[ISTID=$intISTID and ISTMotivationID=$intISTMotivationID]/ISTReasonID">
              <td valign="top" style="width:2%; padding:3px; text-align:center;">
                <input id="chkISTReasonID" name="ISTReasonID" type="checkbox" checked="checked"></input>
              </td>
            </xsl:when>
            <xsl:otherwise>
              <td valign="top" style="width:2%; padding:3px;text-align:center;">
                <input id="chkISTReasonID" name="ISTReasonID" type="checkbox"></input>
              </td>
            </xsl:otherwise>
          </xsl:choose>
              </xsl:for-each>
        </tr>
      </xsl:for-each>
    </table>
      </html>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • &lt;xsl:choose ... &gt; 是你所追求的,但不知道你是如何尝试使用它的,很难知道为什么你不能让它工作。
  • @AdrianWragg 我不知道为什么不显示未选中的复选框
  • 显而易见的答案是,这是因为您的测试始终返回 true - 该节点存在。不幸的是,这里没有足够的信息来复制这个问题。尝试将其重写为演示问题的最小可能方式 - 删除大量不相关的代码,包括 XML 等。
  • 您没有看到其他复选框的原因显然是因为您的测试条件是真实的!
  • @Dlamini.M 请发布一个 reproducible 示例,包括 XML 输入 - 最好将其最小化为重现问题所必需的内容。见:minimal reproducible example

标签: c# linq xslt dataset


【解决方案1】:

问题解决我刚刚在我的查询中执行了左连接并且它正在工作。

 var query2 = (from a in _entities.ISTReasons
                      join b in _entities.ISTMotivations on new { col1 = a.ISTReasonID, col2 = Id } equals new { col1 = b.ISTReasonID, col2 = b.ISTID }
                      into tmp1
                      from c in tmp1.DefaultIfEmpty()
                      select new
                      {
                          ISTReasonID = a.ISTReasonID,                        
                          ReasonDesc = a.LookupDesc,
                          ReasonCode = a.LookupCode,
                          Checked = c.ISTMotivationID != null ? "Y" : "N"
                      });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2021-09-19
    • 2022-01-24
    • 1970-01-01
    • 2022-11-19
    • 2016-06-27
    相关资源
    最近更新 更多