加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用XML和XSL

发布时间:2020-12-16 01:34:50 所属栏目:百科 来源:网络整理
导读:第一次编辑 我正在以C#形式将Child 1标记提取到DropDownList中,Plz建议删除Parent标记的最佳实践代码(C#).它是 XML文件中的所有子标记. 例: Parents Parent Child 1Something/Child 1 Child 2Something/Child 2 Child 3Something/Child 3 Child 4Something/
第一次编辑
我正在以C#形式将Child 1标记提取到DropDownList中,Plz建议删除Parent标记&的最佳实践代码(C#).它是 XML文件中的所有子标记.
例:

<Parents>
      <Parent>
        <Child 1>Something</Child 1>
        <Child 2>Something</Child 2>
        <Child 3>Something</Child 3>
        <Child 4>Something</Child 4>
      </Parent>
      <Parent>
        <Child 1>Something 1</Child 1>
        <Child 2>Something 1</Child 2>
        <Child 3>Something 1</Child 3>
        <Child 4>Something 1</Child 4>
      </Parent>
    </Parents>

—上一个问题—
如何将以下样式表标记插入到使用C#代码创建的新xml文件中?

<?xml-stylesheet type="text/xsl" href="issuez.xsl"?>

用于创建xml文件的C#代码: –

new XDocument(
                        new XElement("issues",new XElement("issue",new XElement("cat",comboBox1.Text),new XElement("desc",richTextBox1.Text),new XElement("end",dateTimePicker1.Text),new XElement("att",textBox2.Text)
                            )
                        )
                        ).Save(path);

解决方法

首先,确保XML中的日期以规范的YYYY-MM-DD格式表示,时间表示为HH:MM:SS,以便XSLT(在1.0中,没有日期或时间数据类型)可以比较并对它们进行排序

其次,使用Steve Muench的分组技术.您使用xsl:key在项目的日期生成一个键.然后可以使用key()函数查找给定日期的所有项目的列表.

使用该键,您可以构建项目中显示的不同日期的列表.这是Muenchian技术:找到列表中第一个项目的每个项目,key()返回该项目的日期.此技术可确保您始终为每个不同的日期值获取一个且仅一个项目.然后,您对这些项目进行排序,并使用它们的日期来推动输出的实际生产.

一个最小的例子:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:key name="dates" match="/data/newsitem" use="@date"/>

  <xsl:template match="/">
    <output>
      <!-- find exactly one newsitem node for each distinct @date in the document -->
      <xsl:for-each select="/data/newsitem[generate-id() = generate-id(key('dates',@date)[1])]">
        <xsl:sort select="@date" order="descending"/>
        <xsl:call-template name="newsitems_for_date">
          <xsl:with-param name="date" select="@date"/>
        </xsl:call-template>
      </xsl:for-each>
    </output>
  </xsl:template>

  <xsl:template name="newsitems_for_date">
    <xsl:param name="date"/>
    <h1>
      <xsl:value-of select="$date"/>
    </h1>
    <xsl:apply-templates select="/data/newsitem[@date=$date]">
       <xsl:sort select="@time" order="descending"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="newsitem">
    <p>
      newsitem for <xsl:value-of select="@date"/>
    </p>
  </xsl:template>

</xsl:stylesheet>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读