[英]Combing multiple arrays in xslt
我有一個要求,其中有一個 XML 結構,其根元素具有 2 個數組類型的子元素和如下所示的示例請求結構
<Root>
<Header>
<Inv>12</Inv>
</Header>
<Detail>
<Val>aa</Val>
<Line>1</Line>
</Detail>
<Header>
<Inv>15</Inv>
</Header>
<Detail>
<Val>bb</Val>
<Line>2</Line>
</Detail>
</Root>
我必須得到如下回應:
<CreateInvoice>
<Data>
<Invoice>
<Inv>12</Inv>
<InvoiceLine>
<Val>aa</Val>
<Line>1</Line>
</InvoiceLine>
</Invoice>
</Data>
<Data>
<Invoice>
<Inv>15</Inv>
<InvoiceLine>
<Val>bb</Val>
<Line>2</Line>
</InvoiceLine>
</Invoice>
</Data>
</CreateInvoice>
我嘗試在數據上使用嵌套的 for-each,但無法獲得響應。 要么只填充 inv,要么填充 InvoiceLine。
如果每張發票只有一個Header
和一個Detail
那么你可以簡單地做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Root">
<CreateInvoice>
<xsl:for-each select="Header">
<Data>
<Invoice>
<xsl:copy-of select="Inv"/>
<InvoiceLine>
<xsl:copy-of select="following-sibling::Detail[1]/*"/>
</InvoiceLine>
</Invoice>
</Data>
</xsl:for-each>
</CreateInvoice>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.