使用 XML &XSL [英] Working with XML & XSL

查看:27
本文介绍了使用 XML &XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次编辑
我正在以 C# 形式将 Child 1 标记提取到 DropDownList 中,请建议删除 Parent 标记的最佳实践代码 (C#) &所有它都是 XML 文件中的子标签.示例:

FIRST EDIT
I'm fetching the Child 1 tag into a DropDownList in a C# form, Plz suggest the best practise code (C#) for deleting the Parent tag & all it's child tags in an XML file. Example:

    <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 文件中????

--- Previous Question ---
How can I insert the following stylesheet tag into my new xml file which is created using C# code????

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

创建xml文件的C#代码:-

C# code to create the xml file:-

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 中,没有日期或时间数据类型)可以对它们进行比较和排序.

First, make sure that dates in your XML are represented in the canonical YYYY-MM-DD format, and times as HH:MM:SS, so that XSLT (which, in 1.0, doesn't have date or time datatypes) can compare and sort them.

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

Second, use Steve Muench's technique for grouping. You generate a key on the items' dates, using xsl:key. The key() function can then be used to find a list of all items on a given date.

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

Using that key, you can build a list of the distinct dates that appear in the items. This is the Muenchian technique: find each item that's the first item in the list that key() returns for that item's date. This technique guarantees that you're always get one and only one item for each distinct date value. You then sort those items, and use their dates to drive the actual production of your output.

一个最小的例子:

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

这篇关于使用 XML &amp;XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆