How to recalculate position, when copy template
I want to ask how to recalculate line number position and other data when I copy template from another xml file if satisfy condition that code should be the same like in lookup.xml.
My programs look like that:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line-Item[code = document('lookup.xml')/*/*/code]" />
<xsl:template match="Line[not(Line-Item/code[not(. = document('lookup.xml')/*/*/code ) ] )]"/>
</xsl:stylesheet>
xml file:
<document>
<header>
<remarks>test</remarks>
</header>
<Line>
<Line-Item>
<lineNumb>1</lineNumb>
<code>123</code>
<amount>4</amount>
</Line-Item>
<Line-Item>
<lineNumb>2</lineNumb>
<code>444</code>
<amount>2</amount>
</Line-Item>
<Line-Item>
<lineNumb>3</lineNumb>
<code>321</code>
<amount>1</amount>
</Line-Item>
</Line>
<summary>
<total-line>3</total-line>
<total-amount>7</total-amount>
</summary>
</document>
Lookup.xml file:
<lookup>
<开发者_如何学JAVA;Codes>
<code>123</code>
</Codes>
</lookup>
I need recalculate lineNumb in Line-Item, And summary there is total-line and total-amount.
Correct result:
<document>
<header>
<remarks>test</remarks>
</header>
<Line>
<Line-Item>
<lineNumb>1</lineNumb>
<code>444</code>
<amount>2</amount>
</Line-Item>
<Line-Item>
<lineNumb>2</lineNumb>
<code>321</code>
<amount>1</amount>
</Line-Item>
</Line>
<summary>
<total-line>2</total-line>
<total-amount>3</total-amount>
</summary>
</document>
Your work is almost done. You need only XPath count()
and sum()
. To recalculate the lineNumb
, I've counted all previous sibling elements but the ones matching the lookup code.
I think this should work fine on the base of your assumptions.
XSLT 1.0 tested on Saxon-B 9.0.0.4J
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line">
<xsl:copy>
<xsl:apply-templates select="Line-Item"/>
</xsl:copy>
<xsl:variable name="lines" select="count(Line-Item[not(code = document('lookup.xml')/*/*/code)])"/>
<xsl:variable name="amount" select="sum(Line-Item[not(code = document('lookup.xml')/*/*/code)]/amount)"/>
<summary>
<total-line><xsl:value-of select="$lines"/></total-line>
<total-amount><xsl:value-of select="$amount"/></total-amount>
</summary>
</xsl:template>
<xsl:template match="Line-Item">
<xsl:copy>
<lineNumb>
<xsl:value-of select="count(preceding-sibling::*[not(code = document('lookup.xml')/*/*/code)])+1"/>
</lineNumb>
<xsl:copy-of select="code"/>
<xsl:copy-of select="amount"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line-Item[code = document('lookup.xml')/*/*/code]|summary" />
</xsl:stylesheet>
精彩评论