[英]global sequence numbering across different for each groups in XSLT 2.0
我必須生成一個xml文件,其中某些分組的元素(分組行的文檔)按順序編號。 這些組又屬於另一個組(按文檔類型)。 無論在哪個組中,在所有文檔中編號都必須是連續的。
請參閱示例輸出中的Documents元素中的DocId屬性:
輸入
<Entries>
<Entry>
<UserID>1</UserID>
<DocNumber>1002</DocNumber>
<Description>An invoice</Description>
<Amount>3103.2000</Amount>
<DocType>INVOICE</DocType>
</Entry>
<Entry>
<UserID>2</UserID>
<DocNumber>3001</DocNumber>
<Description>Some receipt</Description>
<Amount>2352.0000</Amount>
<DocType>RECEIPT</DocType>
</Entry>
<Entry>
<UserID>1</UserID>
<DocNumber>1002</DocNumber>
<Description>An invoice</Description>
<Amount>2861.8400</Amount>
<DocType>INVOICE</DocType>
</Entry>
<Entry>
<UserID>2</UserID>
<DocNumber>3001</DocNumber>
<Description>Some receipt</Description>
<Amount>2352.0000</Amount>
<DocType>RECEIPT</DocType>
</Entry>
<Entry>
<UserID>5</UserID>
<DocNumber>1004</DocNumber>
<Description>Another invoice</Description>
<Amount>2.34</Amount>
<DocType>INVOICE</DocType>
</Entry>
</Entries>
XSLT 2.0
<xsl:stylesheet exclude-result-prefixes="xs xdt err fn" version="2.0" xmlns:err="http://www.w3.org/2005/xqt-errors" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ser="http://webservice">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/Entries">
<ser:Request>
<xsl:for-each-group select="Entry" group-by="DocType">
<xsl:sort select="current-grouping-key()"/>
<ser:ItemFile ImportName="{DocType}" Date="{current-date()}">
<xsl:for-each-group select="current-group()" group-by="DocNumber">
<xsl:sort select="current-grouping-key()"/>
<ser:Documents DocId="{position()}" DocRef="{DocNumber}" Desc="{Description}" >
<xsl:for-each select="current-group()">
<Line amount="{Amount}"/>
</xsl:for-each>
</ser:Documents>
</xsl:for-each-group>
</ser:ItemFile>
</xsl:for-each-group>
</ser:Request>
</xsl:template>
</xsl:stylesheet>
輸出值
<ser:Request xmlns:ser="http://webservice">
<ser:ItemFile ImportName="INVOICE" Date="2017-10-13+02:00">
<ser:Documents DocId="1" DocRef="1002" Desc="An invoice">
<Line amount="3103.2000"/>
<Line amount="2861.8400"/>
</ser:Documents>
<ser:Documents DocId="2" DocRef="1004" Desc="Another invoice">
<Line amount="2.34"/>
</ser:Documents>
</ser:ItemFile>
<ser:ItemFile ImportName="RECEIPT" Date="2017-10-13+02:00">
<ser:Documents DocId="1" DocRef="3001" Desc="Some receipt">
<Line amount="2352.0000"/>
<Line amount="2352.0000"/>
</ser:Documents>
</ser:ItemFile>
</ser:Request>
在此示例中,最后一個DocId屬性的期望輸出將為3,遵循該順序。
我注意到如果我使用position()函數,則會在每個組中重新開始編號,這不是我想要的。 我也嘗試了xsl:number元素,但沒有成功。 我考慮過對第一組中的元素進行計數,然后將其添加到position()中,這對我有限的組有效,但對於可變數量的組卻無法通用。
是否有一些相對簡單的方法來實現這一目標? 先感謝您。
我會將新創建的元素存儲在變量中,然后將其推送通過模板,該模板復制除DocId
屬性之外的所有內容,然后您可以在其中使用xsl:number
進行設置:
<xsl:stylesheet exclude-result-prefixes="xs xdt err fn" version="2.0" xmlns:err="http://www.w3.org/2005/xqt-errors" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ser="http://webservice">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Entries">
<ser:Request>
<xsl:variable name="result">
<xsl:for-each-group select="Entry" group-by="DocType">
<xsl:sort select="current-grouping-key()"/>
<ser:ItemFile ImportName="{DocType}" Date="{current-date()}">
<xsl:for-each-group select="current-group()" group-by="DocNumber">
<xsl:sort select="current-grouping-key()"/>
<ser:Documents DocId="" DocRef="{DocNumber}" Desc="{Description}" >
<xsl:for-each select="current-group()">
<Line amount="{Amount}"/>
</xsl:for-each>
</ser:Documents>
</xsl:for-each-group>
</ser:ItemFile>
</xsl:for-each-group>
</xsl:variable>
<xsl:apply-templates select="$result"/>
</ser:Request>
</xsl:template>
<xsl:template match="ser:Documents/@DocId">
<xsl:attribute name="{name()}">
<xsl:number select=".." level="any"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.