转换 XML 文档以与 .NET 2.0 TreeView 控件一起使用

发布于 2024-07-15 04:37:19 字数 1178 浏览 5 评论 0原文

我有一个格式如下的 XML 文件:

<root>
  <category>
    <doctype>
      <name>Doc1</name>
      <site>
        <name>Site1</name>
        <target>iframe</target>
        <url>http://www.gmail.com</url>
      </site>
    </doctype>
    <doctype>
      <name>Doc2</name>
      <site>
        <name>Site2</name>
        <target>iframe</target>
        <url>http://www.bbc.co.uk</url>
      </site>
    </doctype>
  </category>
</root>

我需要在标准 .net 2.0 TreeView 控件上使用它,该控件需要以下格式的 XML

<root>
  <category>  
    <doctype name="Doc1">
      <site name = "Site1" target = "iframe" url = "http://www.gmail.com">
      </site>
    </doctype>
    <doctype name="Doc2">
      <site name = "Site2" target = "iframe" url = "http://www.bbc.co.uk">
      </site>
    </doctype>
  </category>
</root>

最大的复杂性是 DOCTYPE 节点的某些子节点需要转换为属性(即 NAME),而一些保留为需要自己的属性(即 SITE)的子节点。

如何使用 XSLT 来完成此操作?

I have an XML file which is in the following format:

<root>
  <category>
    <doctype>
      <name>Doc1</name>
      <site>
        <name>Site1</name>
        <target>iframe</target>
        <url>http://www.gmail.com</url>
      </site>
    </doctype>
    <doctype>
      <name>Doc2</name>
      <site>
        <name>Site2</name>
        <target>iframe</target>
        <url>http://www.bbc.co.uk</url>
      </site>
    </doctype>
  </category>
</root>

I need to use it on a standard .net 2.0 TreeView control which requires the XML in the following format

<root>
  <category>  
    <doctype name="Doc1">
      <site name = "Site1" target = "iframe" url = "http://www.gmail.com">
      </site>
    </doctype>
    <doctype name="Doc2">
      <site name = "Site2" target = "iframe" url = "http://www.bbc.co.uk">
      </site>
    </doctype>
  </category>
</root>

The biggest complication is the fact that some child nodes of the DOCTYPE node need to be converted to attributes (i.e. NAME) while some stay as child nodes which require attributes of their own (i.e. SITE).

How can this be done using XSLT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2024-07-22 04:37:19

以下 XSLT 1.0 转换可实现您的预​​期。

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="root | category | doctype | site">
    <xsl:copy>
       <xsl:apply-templates select="*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="name | target | url">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

输出:

<root>
  <category>
    <doctype name="Doc1">
      <site name="Site1" target="iframe" url="http://www.gmail.com"></site>
    </doctype>
    <doctype name="Doc2">
      <site name="Site2" target="iframe" url="http://www.bbc.co.uk"></site>
    </doctype>
  </category>
</root>

The following XSLT 1.0 transformation does what you intend.

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="root | category | doctype | site">
    <xsl:copy>
       <xsl:apply-templates select="*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="name | target | url">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

Output:

<root>
  <category>
    <doctype name="Doc1">
      <site name="Site1" target="iframe" url="http://www.gmail.com"></site>
    </doctype>
    <doctype name="Doc2">
      <site name="Site2" target="iframe" url="http://www.bbc.co.uk"></site>
    </doctype>
  </category>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文