2017-01-02 16 views
-1

у меня есть на входДобавить общее количество и добавить его в новый тег

<Invoice> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>1</Consolidated_Invoice_ID> 
    <Line>1</Line> 
    <Gross_Invoice_Amount>1110</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>1</Consolidated_Invoice_ID> 
    <Line>2</Line> 
    <Gross_Invoice_Amount>1110</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>2</Consolidated_Invoice_ID> 
    <Line>1</Line> 
    <Gross_Invoice_Amount>2220</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>2</Consolidated_Invoice_ID> 
    <Line>2</Line> 
    <Gross_Invoice_Amount>2220</Gross_Invoice_Amount> 
    </Invoice_Details> 
    </Invoice> 

Я хочу вывод в формате:

<Invoice> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>1</Consolidated_Invoice_ID> 
    <Line>1</Line> 
    <Gross_Invoice_Amount>1110</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>1</Consolidated_Invoice_ID> 
    <Line>2</Line> 
    <Gross_Invoice_Amount>1110</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>1</Consolidated_Invoice_ID> 
    <Line>3</Line> 
    <Gross_Invoice_Amount>22220</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>2</Consolidated_Invoice_ID> 
    <Line>1</Line> 
    <Gross_Invoice_Amount>2220</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>2</Consolidated_Invoice_ID> 
    <Line>2</Line> 
    <Gross_Invoice_Amount>2220</Gross_Invoice_Amount> 
    </Invoice_Details> 
    <Invoice_Details> 
    <Consolidated_Invoice_ID>2</Consolidated_Invoice_ID> 
    <Line>3</Line> 
    <Gross_Invoice_Amount>4440</Gross_Invoice_Amount> 
    </Invoice_Details> 
    </Invoice> 

значений счета ID и значение строки должны быть созданы как новые элементы, которые добавляют брутто всех одинаковых идентификаторов.

Условие: Значение брутто требуется только в том случае, если по крайней мере одно значение брутто в любом из доступных идентификаторов. Если брутто не является avilable, то оно не требуется.

+0

добавьте либо XSLT-1.0 тег или XSLT-2.0 и конкретно о версии XSLT, которую вы используете. – Madeyedexter

+0

Soa принимает обе версии..thanks –

ответ

0

Вот XSLT 1.0 решение:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

<xsl:key name="invoices" match="Invoice/Invoice_Details" use="Consolidated_Invoice_ID"/> 
    <xsl:template match="/"> 
     <Invoice> 
     <xsl:for-each select="Invoice/Invoice_Details[count(.|key('invoices',Consolidated_Invoice_ID)[1])=1]"> 
      <xsl:variable name="details" select="key('invoices',Consolidated_Invoice_ID)"/> 
      <xsl:copy-of select="$details"/> 
      <!--Add a new record with gross amount and total--> 
      <Consolidated_Invoice_ID> 
      <xsl:value-of select="Consolidated_Invoice_ID"/> 
      </Consolidated_Invoice_ID> 
      <Line> 
      <xsl:value-of select="count($details)+1"/> 
      </Line> 
      <Gross_Invoice_Amount> 
      <xsl:value-of select="sum($details/Gross_Invoice_Amount)"/> 
      </Gross_Invoice_Amount> 
     </xsl:for-each> 
     </Invoice> 
    </xsl:template> 
</xsl:transform> 
+0

Вот рабочее преобразование: http://xsltransform.net/jz1PuPw – Madeyedexter