需要帮助来创建XSLT

时间:2014-12-30 10:41:25

标签: xml xslt

我有一个从SQL表生成的XML文件。我需要将XML转换为我想要的输出XML。

输入XML:

<rowset>
<row>
<aaa>123</aaa>
<bbb>James</bbb>
<ddd>Large</ddd>
<eee>Black</eee>
<ddd>456213</ddd>
</row>
<row>
<aaa>123</aaa>
<bbb>James</bbb>
<ddd>Large</ddd>
<eee>Blue</eee>
<ddd>456213</ddd>
</row>
<row>
<aaa>123</aaa>
<bbb>James</bbb>
<ddd>small</ddd>
<eee>Black</eee>
<ddd>456213</ddd>
</row>
<row>
<aaa>123</aaa>
<bbb>James</bbb>
<ddd>small</ddd>
<eee>blue</eee>
<ddd>456213</ddd>
</row>
<row>
<aaa>321</aaa>
<bbb>William</bbb>
<ddd>Large</ddd>
<eee>White</eee>
<ddd>555555</ddd>
</row>
<row>
<aaa>321</aaa>
<bbb>William</bbb>
<ddd>Large</ddd>
<eee>Yellow</eee>
<ddd>555555</ddd>
</row>
<row>
<aaa>321</aaa>
<bbb>William</bbb>
<ddd>small</ddd>
<eee>White</eee>
<ddd>555555</ddd>
</row>
<row>
<aaa>321</aaa>
<bbb>William</bbb>
<ddd>small</ddd>
<eee>Yellow</eee>
<ddd>555555</ddd>
</row>
</rowset> 

输出XML:

<?xml version="1.0" encoding="utf-8"?>
<tXML xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
<product product-id = "123">
    <Name>James</Name>
    <Image>
        <Image image-view-type = "Large" image-color = "Black">
        <Image image-view-type = "Large" image-color = "Blue">
        <Image image-view-type = "small" image-color = "Black">
        <Image image-view-type = "small" image-color = "Blue">
    </Image>
    <DeptCode>456213</DeptCode>
</product>
<product product-id = "321">
    <Name>William</Name>
    <Image>
        <Image image-view-type = "Large" image-color = "White">
        <Image image-view-type = "Large" image-color = "Yellow">
        <Image image-view-type = "small" image-color = "White">
        <Image image-view-type = "small" image-color = "Yellow">
    </Image>
    <DeptCode>555555</DeptCode>
</product>
</tXML>

如何为此编写XSLT。 请注意产品很多。所以会有成千上万的产品。

2 个答案:

答案 0 :(得分:1)

要对此XML进行排序,您可以使用Muenchian分组。遵循XSLT

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
  <xsl:key name="prodId" match="row" use="aaa" />
  <xsl:template match="rowset">
    <xsl:for-each select="row[generate-id() =
            generate-id(key('prodId', aaa)[1])]"> 
      <product product-id = "{aaa}">
        <Name><xsl:value-of select="bbb"/></Name> 
        <xsl:for-each select="key('prodId', aaa)">
          <Image>
            <xsl:attribute name="image-view-type" select="ddd[1]"/>
            <xsl:attribute name="image-color" select="eee"/>
          </Image>
        </xsl:for-each>
        <DeptCode><xsl:value-of select="ddd[2]"/></DeptCode>
      </product>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

当应用于您的输入时,XML会生成输出

<product product-id="123">
  <Name>James</Name>
  <Image image-view-type="Large" image-color="Black"/>
  <Image image-view-type="Large" image-color="Blue"/>
  <Image image-view-type="small" image-color="Black"/>
  <Image image-view-type="small" image-color="blue"/>
  <DeptCode>456213</DeptCode>
</product>
<product product-id="321">
  <Name>William</Name>
  <Image image-view-type="Large" image-color="White"/>
  <Image image-view-type="Large" image-color="Yellow"/>
  <Image image-view-type="small" image-color="White"/>
  <Image image-view-type="small" image-color="Yellow"/>
 <DeptCode>555555</DeptCode>
</product>

简短说明:首先定义一个用于排序的键

<xsl:key name="prodId" match="row" use="aaa" />

然后选择此键具有相同值的所有行:

<xsl:for-each select="row[generate-id() =
        generate-id(key('prodId', aaa)[1])]"> 

这两行包含aaa的唯一值 然后迭代行集中与aaa具有相同值的所有行作为当前唯一的prodId:

<xsl:for-each select="key('prodId', aaa)">

Muenchian Grouping在本文中由Jeni Tennison http://www.jenitennison.com/xslt/grouping/muenchian.xml详细描述

作为XSLT分组的附加参考,您可以查看http://www.dpawson.co.uk/xsl/sect2/N4486.html

答案 1 :(得分:0)

希望这有帮助,

  <xsl:key name="data" match="row" use="bbb"/>
  <xsl:output method = "xml" omit-xml-declaration = "no"/>
  <xsl:template match="/*">
    <root>
      <xsl:for-each select="row[count(. | key('data', bbb)[1]) = 1]">
        <product>
          <xsl:attribute name="product-id">
            <xsl:value-of select="aaa"/>
          </xsl:attribute>
          <Name>
            <xsl:value-of select="bbb"/>
          </Name>
          <Image>
            <xsl:for-each select="key('data',bbb)">
              <Image>
                <xsl:attribute name="image-view-type">
                  <xsl:value-of select="ddd"/>
                </xsl:attribute>
                <xsl:attribute name="image-color">
                  <xsl:value-of select="eee"/>
                </xsl:attribute>
              </Image>
            </xsl:for-each>
          </Image>
          <DeptCode>
            <xsl:value-of select="ddd[string(number(.)) != 'NaN']"/>
          </DeptCode>
        </product>
      </xsl:for-each>
    </root>
  </xsl:template>