【问题标题】:xsl transform of dataset.writexml XMLdataset.writexml XML 的 xsl 转换
【发布时间】:2011-01-11 04:36:54
【问题描述】:

我有一个场景,我将 .net 数据集导出到 xml 文件,但我想将 xml 输出的结构转换为更分层的结构。以下是 dataset.xmlwrite() 方法导出的数据集格式。

<NewDataSet>
    <Table>
        <id>100</id>
        <empname>Joe Smith</empname>
        <phone>111-111-1111</phone>
        <mobile>222-222-2222</mobile>

    </Table>
    <Table>
        <id>101</id>
        <empname>Ann Jensen</empname>
        <phone>111-111-0000</phone>
        <mobile>222-222-0000</mobile>
    </Table>
<NewDataSet>

我想把它转换成下面的结构。我是 xsl 转换的新手,我不确定如何防止 &lt;Table&gt; 元素对数据集中的每条记录重复。

<NewDataSet>
    <Table>
        <employee id="100">
            <empname>Joe Smith</empname>
            <phone>111-111-1111</phone>
            <mobile>222-222-2222</mobile>
        </employee>
        <employee id="101">
            <empname>Ann Jensen</empname>
            <phone>111-111-0000</phone>
            <mobile>222-222-0000</mobile>
        </employee>
    </Table>
<NewDataSet>

我尝试使用 xsl:for-each 和 xsl:if 语句的组合来获得我想要的东西,但到目前为止我还不能让它工作。任何帮助将不胜感激。

【问题讨论】:

  • 好问题,+1。有关完全符合 XSLT 精神的简单而强大的解决方案,请参阅我的答案:使用并覆盖标识规则。另请注意,根本没有使用条件 XSLT 指令。 :)

标签: xslt dataset transform writexml


【解决方案1】:

这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="Table[1]">
  <Table>
   <xsl:apply-templates select="../Table/id"/>
  </Table>
 </xsl:template>

 <xsl:template match="Table[position()>1]"/>

 <xsl:template match="Table/id">
  <employee id="{.}">
   <xsl:apply-templates select=
      "following-sibling::node()"/>
  </employee>
 </xsl:template>
</xsl:stylesheet>

应用于所提供的 XML 文档时(更正为格式正确):

<NewDataSet>
    <Table>
        <id>100</id>
        <empname>Joe Smith</empname>
        <phone>111-111-1111</phone>
        <mobile>222-222-2222</mobile>
    </Table>
    <Table>
        <id>101</id>
        <empname>Ann Jensen</empname>
        <phone>111-111-0000</phone>
        <mobile>222-222-0000</mobile>
    </Table>
</NewDataSet>

产生想要的正确结果

<NewDataSet>
   <Table>
      <employee id="100">
         <empname>Joe Smith</empname>
         <phone>111-111-1111</phone>
         <mobile>222-222-2222</mobile>
      </employee>
      <employee id="101">
         <empname>Ann Jensen</empname>
         <phone>111-111-0000</phone>
         <mobile>222-222-0000</mobile>
      </employee>
   </Table>
</NewDataSet>

解释

  1. 身份规则“按原样”复制每个节点。

  2. 第一个 Table 元素与覆盖模板匹配。这会在结果中创建唯一的 Table,并将模板应用于所有 Table 元素的子元素。

  3. id 元素与覆盖模板匹配,该模板将其转换为具有id 属性的employee 元素,其值为id 元素。这也将来自employee 元素内部的模板应用到id 的所有其他同级,并且它们被身份模板按原样复制。

【讨论】:

  • @dvhh:是的,XSLT 是一种极其优雅和强大的语言——很大程度上归功于“身份规则”设计模式。
  • 很明显我有很多关于 XSLT 的知识要学。我了解您所做的大部分工作(感谢您的解释)。但是有一个澄清 - following-sibling::node() 代码选择 id 元素的所有兄弟姐妹 - 而不仅仅是直接的“跟随”兄弟姐妹?我没想到会这样。
  • @Clinemi:是的,following-sibling:: 轴从节点测试中选择 所有 节点,这些节点是上下文节点的兄弟姐妹。如果你只想要后面的第一个兄弟,你可以指定这个,例如:following-sibling::node()[1]
  • @Dimitre:+1 更好的答案。但是有点困惑......像/ - > NewDataSet这样的东西不是更好吗? NewDataSet -> Table; Table -> employee; id -> 什么都没有?
  • @Alejandro:我不确定我是否理解你的问题。我编写此代码的目的是使其尽可能短(模板数量)。
【解决方案2】:

这应该可以解决问题

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <Table>
    <xsl:for-each select="/NewDataSet/Table">
        <employee>
            <xsl:attribute name="id"><xsl:value-of select="id/."/></xsl:attribute>
            <xsl:for-each select="*">
                <xsl:choose>
                    <xsl:when test="name() = 'id' "/>
                    <xsl:otherwise>
                        <xsl:copy-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </employee>
    </xsl:for-each>
    </Table>
</xsl:template>
</xsl:stylesheet>

【讨论】:

  • 别提了,我的回答比@Dimitre的简单一点
  • 谢谢,我更喜欢循环解决方案而不是基于模板的解决方案,因为我可以更容易地理解它们,从而使它们服从我的意愿!
猜你喜欢
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 2012-02-01
  • 2014-07-30
相关资源
最近更新 更多