我已将我的输入xml复制到下面,也是我想要的输出xml。能否帮助我使用Meunchain Grouping获取输出。
首先必须按RebateInvoiceID进行分组,然后在下面的所需输出中显示RebateEventID。
输入XML
<EnterpriseDocument InterfaceName="RebateInvoiceAdjustmentImport" ClientID="1000001" ClientName="XXXX" Version="1.0" CreationSource="File" CreationTimestamp="2012-05-11T09:53:16" xmlns:script="script">
<RawXMLRow RebateInvoiceID="1001878" RebateEventID="1003042" BusinessUnitCode="0402" CategoryID="1000057" PaymentAmount="89"/>
<RawXMLRow RebateInvoiceID="1001878" RebateEventID="1003042" BusinessUnitCode="0405" CategoryID="1000057" PaymentAmount="94.42"/>
<RawXMLRow RebateInvoiceID="1001878" RebateEventID="1003043" BusinessUnitCode="0406" CategoryID="1000057" PaymentAmount="147.5"/>
<RawXMLRow RebateInvoiceID="1001878" RebateEventID="1003048" BusinessUnitCode="0423" CategoryID="1000057" PaymentAmount="97.36"/>
<RawXMLRow RebateInvoiceID="1001879" RebateEventID="1008042" BusinessUnitCode="0433" CategoryID="1000057" PaymentAmount="89"/>
<RawXMLRow RebateInvoiceID="1001879" RebateEventID="1008042" BusinessUnitCode="0434" CategoryID="1000057" PaymentAmount="70.24"/>
<RawXMLRow RebateInvoiceID="1001879" RebateEventID="1008942" BusinessUnitCode="0435" CategoryID="1000057" PaymentAmount="53.08"/>
<RawXMLRow RebateInvoiceID="1001879" RebateEventID="1008942" BusinessUnitCode="0435" CategoryID="1000057" PaymentAmount="59.4"/>
<RawXMLRow RebateInvoiceID="1001978" RebateEventID="2003042" BusinessUnitCode="0437" CategoryID="1000057" PaymentAmount="127.85"/>
<RawXMLRow RebateInvoiceID="1001978" RebateEventID="2003042" BusinessUnitCode="0438" CategoryID="1000057" PaymentAmount="180.04"/>
<RawXMLRow RebateInvoiceID="1001978" RebateEventID="3003042" BusinessUnitCode="0446" CategoryID="1000057" PaymentAmount="146.61"/>
<RawXMLRow RebateInvoiceID="1001978" RebateEventID="3003042" BusinessUnitCode="0448" CategoryID="1000057" PaymentAmount="76.56"/>
</EnterpriseDocument>
所需的输出XML
<EnterpriseDocument InterfaceName="RebateInvoiceAdjustmentImport" ClientID="1000001" ClientName="XXXX" Version="1.0" CreationSource="File" CreationTimestamp="2012-05-10T16:09:07" xmlns:script="script">
<RebateInvoice RebateInvoiceID="1001878">
<RebateProgram RebateEventID="1003042">
<BusinessUnit BusinessUnitCode="0402">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="89"/>
</BusinessUnit>
<BusinessUnit BusinessUnitCode="0405">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="94.42"/>
</BusinessUnit>
</RebateProgram>
<RebateProgram RebateEventID="1003043">
<BusinessUnit BusinessUnitCode="0406">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="147.5"/>
</BusinessUnit>
</RebateProgram>
<RebateProgram RebateEventID="1003048">
<BusinessUnit BusinessUnitCode="0423">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="97.36"/>
</BusinessUnit>
</RebateProgram>
</RebateInvoice>
<RebateInvoice RebateInvoiceID="1001879">
<RebateProgram RebateEventID="1008042">
<BusinessUnit BusinessUnitCode="0433">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="89"/>
</BusinessUnit>
<BusinessUnit BusinessUnitCode="0434">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="70.24"/>
</BusinessUnit>
</RebateProgram>
<RebateProgram RebateEventID="1008942">
<BusinessUnit BusinessUnitCode="0435">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="53.08"/>
</BusinessUnit>
</RebateProgram>
</RebateInvoice>
<RebateInvoice RebateInvoiceID="1001978">
<RebateProgram RebateEventID="2003042">
<BusinessUnit BusinessUnitCode="0437">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="127.85"/>
</BusinessUnit>
<BusinessUnit BusinessUnitCode="0438">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="180.04"/>
</BusinessUnit>
</RebateProgram>
<RebateProgram RebateEventID="3003042">
<BusinessUnit BusinessUnitCode="0446">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="146.61"/>
</BusinessUnit>
<BusinessUnit BusinessUnitCode="0448">
<ItemCategoryPayment CategoryID="1000057" PaymentAmount="76.56"/>
</BusinessUnit>
</RebateProgram>
</RebateInvoice>
</EnterpriseDocument>
感谢您的回复。但我仍然收到一个错误,说我的样式表是空的或XML格式不正确。让我解释一下你的细节。我有一个.js文件,当我双击.js文件时,它将占用该文件夹中的.csv并使用xslt并给我一个.xml输出文件。
/* code for my .js file */ (Please copy this to note pad and save it as
InvoiceAdjustment.js)
showAbsolutePath("InvoiceAdjustment.js");
function showAbsolutePath(path)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = "";
s += fso.GetAbsolutePathName(path);
var foldername = s.substring(0,s.indexOf(path,0));
showFolderFileList(foldername);
}
function showFolderFileList(foldername)
{
var fso, f, fc, filename;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder(foldername);
fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext())
{
filename = "";
filename += fc.item();
if (filename.slice(-4) == ".csv")
{
convertCSVtoXML(foldername, filename);
}
}
fso = null;
}
function convertCSVtoXML(foldername, filename)
{
var objDOMDocument = new ActiveXObject("MSXML2.DOMDocument.4.0");
objDOMDocument.async = false;
//Create Header
objDOMDocument.appendChild(XMLHeader(objDOMDocument));
var objXMLDOMNode = objDOMDocument.documentElement.selectSingleNode("//Document");
// Declare XML object -- this makes it easier to pass as a parameter
var objXML = new Object();
// Open the extracted csv from zip file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var csvFilename = filename;
var tso = fso.OpenTextFile(csvFilename, 1);
var strInput;
// Loop through the file
while(!tso.AtEndOfStream)
{
strInput = tso.ReadLine();
var vInputLine = strInput.split(",");
objXML.a = vInputLine[0];
objXML.b = vInputLine[1];
objXML.c = vInputLine[2];
objXML.d = vInputLine[3];
objXML.e = vInputLine[4];
if (objXML.a != 'RebateInvoiceID')
{
objXMLDOMNode.appendChild(XMLFileNode(objDOMDocument,objXML));
}
}
tso.Close();
// Load Transform file
var TransformXSL = new ActiveXObject("MSXML2.DOMDocument.4.0");
TransformXSL.async = false
TransformXSL.load(foldername + "\\InvAdj.xsl");
// Load XML file and transform it
var TempStagingDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
TempStagingDoc.async = false;
TempStagingDoc.loadXML(objDOMDocument.xml);
var FinalStr = TempStagingDoc.transformNode(TransformXSL);
// Grab just the file name minus any extension
var fn = filename.substring(0, filename.indexOf(".csv"));
tmpxml = fn + ".xml";
// Write out the transformed file. If writing out just the xml file before transform
// FSObject.WriteLine(objDOMDocument.xml);
var FSObject = fso.CreateTextFile(tmpxml, true);
FSObject.WriteLine(FinalStr);
FSObject.Close();
// Clear all objects
objDOMDocument = null;
fso = null;
TransformXSL = null;
objXML = null;
TempStagingDoc = null;
}
function XMLHeader(objDOMDocument)
{
var XMLHead;
XMLHead = objDOMDocument.createNode(1, "Document","");
var objXMLDOMAttribute = objDOMDocument.createAttribute("Version");
objXMLDOMAttribute.text = "1.0";
XMLHead.attributes.setNamedItem(objXMLDOMAttribute);
var objXMLDOMAttribute = objDOMDocument.createAttribute("CreationTimestamp");
objXMLDOMAttribute.text = getTimestamp();
XMLHead.attributes.setNamedItem(objXMLDOMAttribute);
return(XMLHead);
}
function XMLFileNode(objDOMDocument,objXML)
{
var objXMLDOMNode = objDOMDocument.createNode(1, "RawXMLRow","");
objXMLDOMElement = objDOMDocument.createElement("RebateInvoiceID");
objXMLDOMElement.text = objXML.a;
objXMLDOMNode.appendChild(objXMLDOMElement);
objXMLDOMElement = objDOMDocument.createElement("RebateEventID");
objXMLDOMElement.text = objXML.b;
objXMLDOMNode.appendChild(objXMLDOMElement);
objXMLDOMElement = objDOMDocument.createElement("BusinessUnitCode");
function getBusinessUnitCode(){
var r = String(objXML.c).length;
if(r == 4)
return String(objXML.c);
if( r == 3)
return "0"+String(objXML.c);
if(r == 2){
return "00"+String(objXML.c);
}
else
return "000"+String(objXML.c);
}
objXMLDOMElement.text = getBusinessUnitCode();
objXMLDOMNode.appendChild(objXMLDOMElement);
objXMLDOMElement = objDOMDocument.createElement("CategoryID");
objXMLDOMElement.text = objXML.d;
objXMLDOMNode.appendChild(objXMLDOMElement);
objXMLDOMElement = objDOMDocument.createElement("PaymentAmount");
objXMLDOMElement.text = objXML.e;
objXMLDOMNode.appendChild(objXMLDOMElement);
return(objXMLDOMNode);
}
function getTimestamp()
{
var d = new Date();
var mm = padZeros(d.getMonth()+1);
var dd = padZeros(d.getDate());
var hh = padZeros(d.getHours());
var mn = padZeros(d.getMinutes());
var ss = padZeros(d.getSeconds());
return d.getFullYear()+"-"+ mm +"-"+ dd +"T"+ hh +":"+ mm +":"+ ss;
}
function padZeros(s)
{
if (s < 10)
{
s = "0" + s;
}
return(s);
}
function getDate(stringDate)
{
var a = String(stringDate).split( "/" );
var strXML ;
strDate = "" ;
for ( var i = 0 ; i < a.length; i++ )
{
if (a[i].length == 1)
strDate += "0" + a[i];
else
strDate += a[i];
}
strDate += "" ;
return strDate;
}
/* code for xslt */ ( make an xslt using below code and save it as InvAdj.xsl)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:script="script" exclude-result-
prefixes="msxsl">
<xsl:output omit-xml-declaration="yes"/>
<!-- ROOT -->
<xsl:template match="/">
<EnterpriseDocument>
<xsl:attribute name="InterfaceName">RebateInvoiceAdjustmentImport</xsl:attribute>
<xsl:attribute name="ClientID">
<xsl:choose>
<xsl:when test="normalize-space(ClientID)">
<xsl:value-of select="//Document/RawXMLRow/ClientID"/>
</xsl:when>
<xsl:otherwise>1000001</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="ClientName">XXXX</xsl:attribute>
<xsl:attribute name="Version">1.0</xsl:attribute>
<xsl:attribute name="CreationSource">File</xsl:attribute>
<xsl:attribute name="CreationTimestamp">
<xsl:value-of select="script:getTimestamp()"/>
</xsl:attribute>
<xsl:call-template name="InvAdjData"/>
</EnterpriseDocument>
</xsl:template>
<xsl:template name="InvAdjData">
<xsl:for-each select="//Document/RawXMLRow[not(RebateInvoiceID=preceding-
sibling::RawXMLRow/RebateInvoiceID)]">
<xsl:sort select="RebateInvoiceID"/>
<RebateInvoice>
<xsl:attribute name="RebateInvoiceID">
<xsl:value-of select="RebateInvoiceID"/>
</xsl:attribute>
<xsl:variable name="InvoiceID">
<xsl:value-of select="RebateInvoiceID"/>
</xsl:variable>
<xsl:for-each select="//Document/RawXMLRow[not(RebateEventID=preceding-
sibling::RawXMLRow[RebateInvoiceID=$InvoiceID]/RebateEventID)]">
<xsl:if test="RebateInvoiceID=$InvoiceID">
<xsl:variable name="EventID">
<xsl:value-of select="RebateEventID"/>
</xsl:variable>
<RebateProgram>
<xsl:attribute name="RebateEventID">
<xsl:value-of select="RebateEventID"/>
</xsl:attribute>
<xsl:for-each select="//Document/RawXMLRow[not(BusinessUnitCode=preceding-
sibling::RawXMLRow[RebateInvoiceID=$InvoiceID and
RebateEventID=$EventID]/BusinessUnitCode)]">
<xsl:if test="RebateInvoiceID=$InvoiceID and RebateEventID=$EventID">
<xsl:variable name="BUCode">
<xsl:value-of select="BusinessUnitCode" />
</xsl:variable>
<BusinessUnit>
<xsl:attribute name="BusinessUnitCode">
<xsl:value-of select="BusinessUnitCode"/>
</xsl:attribute>
<xsl:for-each select="//Document/RawXMLRow[not(CategoryID=preceding-
sibling::RawXMLRow[RebateInvoiceID=$InvoiceID and RebateEventID=$EventID and
BusinessUnitCode=$BUCode]/CategoryID)]">
<xsl:if test="RebateInvoiceID=$InvoiceID and RebateEventID=$EventID
and BusinessUnitCode=$BUCode">
<ItemCategoryPayment>
<xsl:attribute name="CategoryID">
<xsl:value-of select="CategoryID"/>
</xsl:attribute>
<xsl:attribute name="PaymentAmount">
<xsl:value-of select="PaymentAmount"/>
</xsl:attribute>
</ItemCategoryPayment>
</xsl:if>
</xsl:for-each>
</BusinessUnit >
</xsl:if>
</xsl:for-each>
</RebateProgram >
</xsl:if>
</xsl:for-each>
</RebateInvoice >
</xsl:for-each>
</xsl:template>
<!-- BEGIN JSCRIPT HELPER CODE, KEEP THIS SECTION AS SMALL AS POSSIBLE IT'S A
PERFORMANCE HOG -->
<msxsl:script language="JScript" implements-prefix="script">
<![CDATA[
function getTimestamp()
{
var d = new Date();
var m, dy, hr, mn, se;
if (d.getMonth()+1 < 10)
{
m = "0"+(d.getMonth()+1);
}
else if (d.getMonth()+1 > 9)
{
m = d.getMonth()+1;
}
if (d.getDate() < 10)
{
dy = "0"+(d.getDate());
}
else if (d.getDate() > 9)
{
dy = d.getDate();
}
if (d.getHours() < 10)
{
hr = "0"+(d.getHours());
}
else if (d.getHours() > 9)
{
hr = d.getHours();
}
if (d.getMinutes() < 10)
{
mn = "0"+(d.getMinutes());
}
else if (d.getMinutes() > 9)
{
mn = d.getMinutes();
}
if (d.getSeconds() < 10)
{
se = "0"+(d.getSeconds());
}
else if (d.getSeconds() > 9)
{
se = d.getSeconds();
}
return d.getFullYear()+"-"+ m +"-"+ dy +"T"+ hr +":"+ mn +":"+ se;
}
]]>
</msxsl:script>
Now copy this records into an excel sheet and save it as .csv
RebateInvoiceID RebateEventID BusinessUnitCode CategoryID PaymentAmount
1001878 1003042 402 1000057 89
1001878 1003042 405 1000057 94.42
1001878 1003043 406 1000057 147.5
1001878 1003048 423 1000057 97.36
1001879 1008042 433 1000057 89
1001879 1008042 434 1000057 70.24
1001879 1008942 435 1000057 53.08
1001978 2003042 437 1000057 127.85
1001978 2003042 438 1000057 180.04
1001978 3003042 446 1000057 146.61
1001978 3003042 448 1000057 76.56
现在将.csv输入文件和.js文件以及xslt放入一个文件夹中,然后双击.js文件,它将生成一个.xml文件。
现在我需要在xslt代码中使用muenchain分组方法获得相同的结果。你能帮我吗。
感谢。
答案 0 :(得分:2)
以下是样式表示例:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="k1" match="RawXMLRow" use="@RebateInvoiceID"/>
<xsl:key name="k2" match="RawXMLRow" use="concat(@RebateInvoiceID, '|', @RebateEventID)"/>
<xsl:template match="EnterpriseDocument">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="RawXMLRow[generate-id() = generate-id(key('k1', @RebateInvoiceID)[1])]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RawXMLRow">
<RebateInvoice RebateInvoiceID="{@RebateInvoiceID}">
<xsl:apply-templates select="key('k1', @RebateInvoiceID)[generate-id() = generate-id(key('k2', concat(@RebateInvoiceID, '|', @RebateEventID))[1])]" mode="evt"/>
</RebateInvoice>
</xsl:template>
<xsl:template match="RawXMLRow" mode="evt">
<RebateProgram RebateEventID="{@RebateEventID}">
<xsl:apply-templates select="key('k2', concat(@RebateInvoiceID, '|', @RebateEventID))" mode="bus"/>
</RebateProgram>
</xsl:template>
<xsl:template match="RawXMLRow" mode="bus">
<BusinessUnit BusinessUnitCode="{@BusinessUnitCode}">
<ItemCategoryPayment CategoryID="{@CategoryID}" PaymentAmount="{@CategoryID}"/>
</BusinessUnit>
</xsl:template>
</xsl:stylesheet>