目录

使用notepad++查看xsl效果

xml文件

shop.xsl文件

输出结果


通过key() 函数通过使用由 <xsl:key> 元素规定的索引号,从文档中返回节点集。

再通过 | 类似于linux的管道,对数据进行二次、三次的节点处理

Linux 管道使用竖线|连接多个命令,这被称为管道符。Linux 管道的具体语法格式如下:
command1 | command2
command1 | command2 [ | commandN... ]

当在两个命令之间设置管道时,管道符|左边命令的输出就变成了右边命令的输入。只要第一个命令向标准输出写入,而第二个命令是从标准输入读取,那么这两个命令就可以形成一个管道。大部分的 Linux 命令都可以用来形成管道。
这里需要注意,command1 必须有正确输出,而 command2 必须可以处理 command2 的输出结果;而且 command2 只能处理 command1 的正确输出结果,不能处理 command1 的错误信息。(来源网络侵删)

另外一个是使用apply-templates 进行模板嵌套

<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点。

分类之后会以此按照,Shop的序号、Date日期、customerID进行排序分组

演示效果可以到如下在线网站检验,如果加载不出来,就加个梯子

.NET XSLT Fiddle (liberty-development.net)https://xsltfiddle.liberty-development.net/

使用notepad++查看xsl效果

如果是使用notepad++

打开xml文件,其中xml文件在<?xml version="1.0"?>下面添加一句

当然如果打开的是xsl则要绑定对应的xml文件才能显示内容,

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

"./"表示当前文件夹下的,添加shop.xsl ,应用上xsl样式,

选择对应的xsl,最后点击transform即可查看效果

 

 

xml文件

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="./shop.xsl"?>
<transactions>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>8BLOJZWL</customerID>
		<giftShop>1</giftShop>
		<transactionID>BjYAtd7lmEOlQUVy</transactionID>
		<value currency="gbp">61.33</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>DR18OCFI</customerID>
		<giftShop>2</giftShop>
		<transactionID>aQ27lMvfnlzY4mkx</transactionID>
		<value currency="gbp">54.21</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>WLVPLUMM</customerID>
		<giftShop>3</giftShop>
		<transactionID>TNSypVVOi8LjTeC2</transactionID>
		<value currency="gbp">30.1</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>31F9PCQD</customerID>
		<giftShop>2</giftShop>
		<transactionID>1ygOIpJ6Tf2HB72e</transactionID>
		<value currency="gbp">57.43</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>9B237CBO</customerID>
		<giftShop>3</giftShop>
		<transactionID>DJs5rDV21qeIuKnb</transactionID>
		<value currency="gbp">10.09</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>FDCXOH0M</customerID>
		<giftShop>2</giftShop>
		<transactionID>2lZ75q0FrSpcREjs</transactionID>
		<value currency="gbp">49.49</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>9VDK67SH</customerID>
		<giftShop>3</giftShop>
		<transactionID>CwNGDKipz6ZAE0NZ</transactionID>
		<value currency="gbp">36.08</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>3QCLVPRM</customerID>
		<giftShop>3</giftShop>
		<transactionID>SKeUsYKGg8WO5kLv</transactionID>
		<value currency="gbp">18.18</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>BQ0JG15U</customerID>
		<giftShop>1</giftShop>
		<transactionID>xiFZUFOv8J8zKqyU</transactionID>
		<value currency="gbp">19.76</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>BA1LBL11</customerID>
		<giftShop>2</giftShop>
		<transactionID>oTfh5Dg0ACCGG0SH</transactionID>
		<value currency="gbp">8.59</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>4FI1TAJW</customerID>
		<giftShop>1</giftShop>
		<transactionID>7Yog0IVnlVbOGIQX</transactionID>
		<value currency="gbp">54.12</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>DOWK75O4</customerID>
		<giftShop>2</giftShop>
		<transactionID>xNcmC90pdXYcdUNF</transactionID>
		<value currency="gbp">72.96</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>BQ893RTX</customerID>
		<giftShop>3</giftShop>
		<transactionID>1eACamM3fu7PqcmX</transactionID>
		<value currency="gbp">27.87</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>KKDLTC4B</customerID>
		<giftShop>2</giftShop>
		<transactionID>4oZAWTFBGOuNtjm6</transactionID>
		<value currency="gbp">56.13</value>
	</transaction>
	<transaction>
		<transaction_date>01/11/2019</transaction_date>
		<customerID>OT2C1KKP</customerID>
		<giftShop>3</giftShop>
		<transactionID>LEgqnA5tqhgkkhQ8</transactionID>
		<value currency="gbp">28.92</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>6AQU1HBA</customerID>
		<giftShop>2</giftShop>
		<transactionID>eb7zMnKoshn3NZBi</transactionID>
		<value currency="gbp">12.75</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>FX6LBLQB</customerID>
		<giftShop>3</giftShop>
		<transactionID>BmFX7FGVu35obyBs</transactionID>
		<value currency="gbp">68.47</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>OS2M5PKJ</customerID>
		<giftShop>3</giftShop>
		<transactionID>UWMWF82vkYvh5dMQ</transactionID>
		<value currency="gbp">63.97</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>A25S3UU6</customerID>
		<giftShop>1</giftShop>
		<transactionID>IX9UspYJgYTYwtbI</transactionID>
		<value currency="gbp">13.84</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>K8MI3QNV</customerID>
		<giftShop>2</giftShop>
		<transactionID>LDssnuvX8yr9U8W0</transactionID>
		<value currency="gbp">9.51</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>QQASCC8J</customerID>
		<giftShop>1</giftShop>
		<transactionID>1Lng8B0uDphSQhu4</transactionID>
		<value currency="gbp">0.89</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>ZIT8QPN5</customerID>
		<giftShop>2</giftShop>
		<transactionID>9MxQ2jmzMLFY1Xae</transactionID>
		<value currency="gbp">1.33</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>ASZN4HEG</customerID>
		<giftShop>3</giftShop>
		<transactionID>yXM4b08chJHcRcPv</transactionID>
		<value currency="gbp">6.29</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>7RI52S4J</customerID>
		<giftShop>2</giftShop>
		<transactionID>jOuIoNvwJbvPOk8o</transactionID>
		<value currency="gbp">41.75</value>
	</transaction>
	<transaction>
		<transaction_date>02/11/2019</transaction_date>
		<customerID>EL7A3UYF</customerID>
		<giftShop>3</giftShop>
		<transactionID>LdIXj8mO6xBJq6dt</transactionID>
		<value currency="gbp">5.93</value>
	</transaction>
</transactions>

shop.xsl文件

其中注释的部分为测试内容,不用理会,可以自行删除

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <!-- <xsl:param name="file2" select="'retail_customers.xml'" /> -->

    <xsl:key name="key1" match="transaction" use="giftShop" />
    <xsl:key name="key2" match="transaction" use="concat(giftShop, '|', transaction_date)" />
    <xsl:key name="key3" match="transaction" use="concat(giftShop, '|', transaction_date, '|', customerID)" />

    <xsl:template match="/transactions">
            <xsl:apply-templates select="transaction[generate-id() = generate-id(key('key1', giftShop)[1])]" mode="key1" />
    </xsl:template>

    <xsl:template match="transaction" mode="key1">
        <Transactions>
            <Shop>
                <xsl:value-of select="giftShop" />
            </Shop>
            <xsl:apply-templates select="key('key1', giftShop)[generate-id() = generate-id(key('key2', concat(giftShop, '|', transaction_date))[1])]" mode="key2" />
        </Transactions>
    </xsl:template>

    <xsl:template match="transaction" mode="key2">
        <br>
            <Date>
                <xsl:value-of select="transaction_date" />
            </Date>
        </br>
        <xsl:apply-templates select="key('key2', concat(giftShop, '|', transaction_date))[generate-id() = generate-id(key('key3', concat(giftShop, '|', transaction_date, '|', customerID))[1])]" mode="key3" />
    </xsl:template>

    <xsl:template match="transaction" mode="key3">
        <Transcation>
            <!-- <xsl:copy-of select="customerID" /> -->
            <xsl:copy-of select="transactionID" />
            <xsl:copy-of select="value" />
            <!-- <xsl:apply-templates select="customers/customer" /> -->
            <xsl:apply-templates select="key('key3', concat(giftShop, '|', transaction_date, '|', customerID))" />
        </Transcation>
    </xsl:template>

    <xsl:template match="transaction">
        <Cunstomer>
            <xsl:copy-of select="customerID" />
        </Cunstomer>
    </xsl:template>

</xsl:stylesheet>

输出结果

<?xml version="1.0" encoding="UTF-8"?>
<Transactions>
   <Shop>1</Shop>
   <br>
      <Date>01/11/2019</Date>
   </br>
   <Transcation>
      <transactionID>BjYAtd7lmEOlQUVy</transactionID>
      <value currency="gbp">61.33</value>
      <Cunstomer>
         <customerID>8BLOJZWL</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>xiFZUFOv8J8zKqyU</transactionID>
      <value currency="gbp">19.76</value>
      <Cunstomer>
         <customerID>BQ0JG15U</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>7Yog0IVnlVbOGIQX</transactionID>
      <value currency="gbp">54.12</value>
      <Cunstomer>
         <customerID>4FI1TAJW</customerID>
      </Cunstomer>
   </Transcation>
   <br>
      <Date>02/11/2019</Date>
   </br>
   <Transcation>
      <transactionID>IX9UspYJgYTYwtbI</transactionID>
      <value currency="gbp">13.84</value>
      <Cunstomer>
         <customerID>A25S3UU6</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>1Lng8B0uDphSQhu4</transactionID>
      <value currency="gbp">0.89</value>
      <Cunstomer>
         <customerID>QQASCC8J</customerID>
      </Cunstomer>
   </Transcation>
</Transactions>
<Transactions>
   <Shop>2</Shop>
   <br>
      <Date>01/11/2019</Date>
   </br>
   <Transcation>
      <transactionID>aQ27lMvfnlzY4mkx</transactionID>
      <value currency="gbp">54.21</value>
      <Cunstomer>
         <customerID>DR18OCFI</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>1ygOIpJ6Tf2HB72e</transactionID>
      <value currency="gbp">57.43</value>
      <Cunstomer>
         <customerID>31F9PCQD</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>2lZ75q0FrSpcREjs</transactionID>
      <value currency="gbp">49.49</value>
      <Cunstomer>
         <customerID>FDCXOH0M</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>oTfh5Dg0ACCGG0SH</transactionID>
      <value currency="gbp">8.59</value>
      <Cunstomer>
         <customerID>BA1LBL11</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>xNcmC90pdXYcdUNF</transactionID>
      <value currency="gbp">72.96</value>
      <Cunstomer>
         <customerID>DOWK75O4</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>4oZAWTFBGOuNtjm6</transactionID>
      <value currency="gbp">56.13</value>
      <Cunstomer>
         <customerID>KKDLTC4B</customerID>
      </Cunstomer>
   </Transcation>
   <br>
      <Date>02/11/2019</Date>
   </br>
   <Transcation>
      <transactionID>eb7zMnKoshn3NZBi</transactionID>
      <value currency="gbp">12.75</value>
      <Cunstomer>
         <customerID>6AQU1HBA</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>LDssnuvX8yr9U8W0</transactionID>
      <value currency="gbp">9.51</value>
      <Cunstomer>
         <customerID>K8MI3QNV</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>9MxQ2jmzMLFY1Xae</transactionID>
      <value currency="gbp">1.33</value>
      <Cunstomer>
         <customerID>ZIT8QPN5</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>jOuIoNvwJbvPOk8o</transactionID>
      <value currency="gbp">41.75</value>
      <Cunstomer>
         <customerID>7RI52S4J</customerID>
      </Cunstomer>
   </Transcation>
</Transactions>
<Transactions>
   <Shop>3</Shop>
   <br>
      <Date>01/11/2019</Date>
   </br>
   <Transcation>
      <transactionID>TNSypVVOi8LjTeC2</transactionID>
      <value currency="gbp">30.1</value>
      <Cunstomer>
         <customerID>WLVPLUMM</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>DJs5rDV21qeIuKnb</transactionID>
      <value currency="gbp">10.09</value>
      <Cunstomer>
         <customerID>9B237CBO</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>CwNGDKipz6ZAE0NZ</transactionID>
      <value currency="gbp">36.08</value>
      <Cunstomer>
         <customerID>9VDK67SH</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>SKeUsYKGg8WO5kLv</transactionID>
      <value currency="gbp">18.18</value>
      <Cunstomer>
         <customerID>3QCLVPRM</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>1eACamM3fu7PqcmX</transactionID>
      <value currency="gbp">27.87</value>
      <Cunstomer>
         <customerID>BQ893RTX</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>LEgqnA5tqhgkkhQ8</transactionID>
      <value currency="gbp">28.92</value>
      <Cunstomer>
         <customerID>OT2C1KKP</customerID>
      </Cunstomer>
   </Transcation>
   <br>
      <Date>02/11/2019</Date>
   </br>
   <Transcation>
      <transactionID>BmFX7FGVu35obyBs</transactionID>
      <value currency="gbp">68.47</value>
      <Cunstomer>
         <customerID>FX6LBLQB</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>UWMWF82vkYvh5dMQ</transactionID>
      <value currency="gbp">63.97</value>
      <Cunstomer>
         <customerID>OS2M5PKJ</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>yXM4b08chJHcRcPv</transactionID>
      <value currency="gbp">6.29</value>
      <Cunstomer>
         <customerID>ASZN4HEG</customerID>
      </Cunstomer>
   </Transcation>
   <Transcation>
      <transactionID>LdIXj8mO6xBJq6dt</transactionID>
      <value currency="gbp">5.93</value>
      <Cunstomer>
         <customerID>EL7A3UYF</customerID>
      </Cunstomer>
   </Transcation>
</Transactions>

Logo

加入社区!打开量化的大门,首批课程上线啦!

更多推荐