使用 XSLT 2.0 和对一个元素进行分组,使用属性重现 XML
Reproduce XML with attributes using XSLT 2.0 & Grouping one element
我是 XSLT 的新手,不得不 group/sum 一个元素,因为一个员工可能有两行数据。我能够使用 tag 和 sum(current-group()) 函数实现这一点。输入 xml 具有属性,并且先前的需要还必须在结果输出 xml.
中重现 xml 属性
**Sample XML:**
<ws:Review>
<ws:Employee>
<ws:EmpID>12345</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Amount>5</ws:Amount>
<ws:Currency Descriptor="IDR">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">IDR</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>12345</ws:EmpID>
<ws:Amount>6</ws:Amount>
<ws:Currency Descriptor="IDR">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">IDR</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>23456</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Currency Descriptor="GBP">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e5</ws:ID>
<ws:ID ws:type="Currency_ID">GBP</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>34567</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Amount>5</ws:Amount>
<ws:Currency Descriptor="USD">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f679</ws:ID>
<ws:ID ws:type="Currency_ID">USD</ws:ID>
</ws:Currency>
</ws:Employee>
</ws:Review>
预期输出:
<ws:Review>
<ws:Employee>
<ws:EmpID>12345</ws:EmpID>
<ws:Amount>15</ws:Amount>
<ws:Currency Descriptor="IDR">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">IDR</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>23456</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Currency Descriptor="GBP">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">GBP</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>34567</ws:EmpID>
<ws:Amount>9</ws:Amount>
<ws:Currency Descriptor="USD">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">USD</ws:ID>
</ws:Currency>
</ws:Employee>
</ws:Review>
我写的XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ws="namespace" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<ws:Review>
<xsl:for-each-group select="ws:Review/ws:Employee" group-by="ws:Employee/ws:EmpID>
<ws:EmpID><xsl:value-of select="ws:Employee/ws:EmpID"/></ws:EmpID>
<ws:Currency><xsl:value-of select="ws:Employee/ws:Currency/@ws:Descriptor"/></ws:Currency>
<ws:Amount><xsl:value-of select="sum(current-group()/ws:Employee/ws:Amount)"/></ws:Amount>
</xsl:for-each-group>
</ws:Review>
</xsl:template>
谁能帮我修改当前的xsl,使属性继承?我很乐意提供任何帮助。
谢谢!
维维克
我会将元素推送到恒等变换,请参阅 http://xsltransform.net/bdxtqy
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:ws="http://example.com/">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ws:Review">
<xsl:copy>
<xsl:for-each-group select="ws:Employee" group-by="ws:EmpID">
<xsl:copy>
<xsl:apply-templates select="ws:EmpID"/>
<ws:Amount><xsl:value-of select="sum(current-group()/ws:Amount)"/></ws:Amount>
<xsl:apply-templates select="* except (ws:EmpID, ws:Amount)"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>
我是 XSLT 的新手,不得不 group/sum 一个元素,因为一个员工可能有两行数据。我能够使用 tag 和 sum(current-group()) 函数实现这一点。输入 xml 具有属性,并且先前的需要还必须在结果输出 xml.
中重现 xml 属性**Sample XML:**
<ws:Review>
<ws:Employee>
<ws:EmpID>12345</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Amount>5</ws:Amount>
<ws:Currency Descriptor="IDR">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">IDR</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>12345</ws:EmpID>
<ws:Amount>6</ws:Amount>
<ws:Currency Descriptor="IDR">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">IDR</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>23456</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Currency Descriptor="GBP">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e5</ws:ID>
<ws:ID ws:type="Currency_ID">GBP</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>34567</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Amount>5</ws:Amount>
<ws:Currency Descriptor="USD">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f679</ws:ID>
<ws:ID ws:type="Currency_ID">USD</ws:ID>
</ws:Currency>
</ws:Employee>
</ws:Review>
预期输出:
<ws:Review>
<ws:Employee>
<ws:EmpID>12345</ws:EmpID>
<ws:Amount>15</ws:Amount>
<ws:Currency Descriptor="IDR">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">IDR</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>23456</ws:EmpID>
<ws:Amount>4</ws:Amount>
<ws:Currency Descriptor="GBP">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">GBP</ws:ID>
</ws:Currency>
</ws:Employee>
<ws:Employee>
<ws:EmpID>34567</ws:EmpID>
<ws:Amount>9</ws:Amount>
<ws:Currency Descriptor="USD">
<ws:ID ws:type="AID">9464cef721784d6ab96eaad1b366f6e7</ws:ID>
<ws:ID ws:type="Currency_ID">USD</ws:ID>
</ws:Currency>
</ws:Employee>
</ws:Review>
我写的XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ws="namespace" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<ws:Review>
<xsl:for-each-group select="ws:Review/ws:Employee" group-by="ws:Employee/ws:EmpID>
<ws:EmpID><xsl:value-of select="ws:Employee/ws:EmpID"/></ws:EmpID>
<ws:Currency><xsl:value-of select="ws:Employee/ws:Currency/@ws:Descriptor"/></ws:Currency>
<ws:Amount><xsl:value-of select="sum(current-group()/ws:Employee/ws:Amount)"/></ws:Amount>
</xsl:for-each-group>
</ws:Review>
</xsl:template>
谁能帮我修改当前的xsl,使属性继承?我很乐意提供任何帮助。
谢谢! 维维克
我会将元素推送到恒等变换,请参阅 http://xsltransform.net/bdxtqy
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:ws="http://example.com/">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ws:Review">
<xsl:copy>
<xsl:for-each-group select="ws:Employee" group-by="ws:EmpID">
<xsl:copy>
<xsl:apply-templates select="ws:EmpID"/>
<ws:Amount><xsl:value-of select="sum(current-group()/ws:Amount)"/></ws:Amount>
<xsl:apply-templates select="* except (ws:EmpID, ws:Amount)"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>