>我需要仅包含来自 E0 兄弟的所有 01 个 recordCode 节点,但需要忽略 E2 和 E3 下的节点,我使用以下代码获得所有 01 后跟 E0。不确定如何创建上面的密钥,这只会导致一个节点的所有 01 都只遵循 E0。这是我目前的 xslt。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Generate keys for Approver, VAT, and Narrative Arrays -->
<xsl:key name="kApproverDetailArray" match="faRecord[recordCode='01']" use="generate-id(preceding-sibling::faRecord[recordCode='E0'][1])" />
<xsl:key name="kVATDetailArray" match="faRecord[recordCode='02']" use="generate-id(preceding-sibling::faRecord[recordCode='E0'][1])" />
<xsl:key name="kNarrativeArray" match="faRecord[recordCode='03']" use="generate-id(preceding-sibling::faRecord[recordCode='E0'][1])" />
<xsl:key name="kItemWorkCodeNarrativeArray" match="faRecord[recordCode='01']" use="generate-id(preceding-sibling::faRecord[recordCode='E2'][1])" />
<xsl:key name="kOrderRecordApproverArray" match="faRecord[recordCode='01']" use="generate-id(preceding-sibling::faRecord[recordCode='E3'][1])" />
<xsl:template match="InvoiceDisplayRequest">
<MCSResponse>
<InvoiceDisplayResponse>
<xsl:apply-templates select="*/faRecord[recordCode='E0']" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="facXML/faRecord[recordCode='E1']" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="facXML/faRecord[recordCode='E3']" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="facXML/faRecord[recordCode='E2']" mode="InvoiceDisplayResponse" />
</InvoiceDisplayResponse>
</MCSResponse>
</xsl:template>
<xsl:template match="faRecord[recordCode='E0']" mode="InvoiceDisplayResponse">
<InvoiceDisplayDetail>
<xsl:apply-templates select="item" mode="InvoiceDisplayDetail" />
<xsl:apply-templates select="key('kApproverDetailArray', generate-id())" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="key('kVATDetailArray', generate-id())" mode="InvoiceDisplayResponse"/>
<xsl:apply-templates select="key('kNarrativeArray', generate-id())" mode="InvoiceDisplayResponse"/>
</InvoiceDisplayDetail>
</xsl:template>
<!-- InvoiceDisplayDetail fields -->
<xsl:template match="item[itemMapCode='1']" mode="InvoiceDisplayDetail">
<LogNumber>
<xsl:value-of select="./itemValue" />
</LogNumber>
</xsl:template>
<xsl:template match="item[itemMapCode='2']" mode="InvoiceDisplayDetail">
<LogAddedDate>
<xsl:value-of select="./itemValue" />
</LogAddedDate>
</xsl:template>
<!-- Approver Array fields -->
<xsl:template match="faRecord[recordCode='01']" mode="InvoiceDisplayResponse">
<ApproverArray>
<xsl:apply-templates select="item" mode="ApproverArray" />
</ApproverArray>
</xsl:template>
<!-- VAT Array fields -->
<xsl:template match="faRecord[recordCode='02']" mode="InvoiceDisplayResponse">
<VATDetailArray>
<xsl:apply-templates select="item" mode="VATDetailArray" />
</VATDetailArray>
</xsl:template>
<xsl:template match="item[itemMapCode='1']" mode="VATDetailArray">
<VATSequence>
<xsl:value-of select="./itemValue" />
</VATSequence>
</xsl:template>
<!-- Narrative Array fields -->
<xsl:template match="faRecord[recordCode='03']" mode="InvoiceDisplayResponse">
<NarrativeArray>
<xsl:apply-templates select="item" mode="NarrativeArray" />
</NarrativeArray>
</xsl:template>
<xsl:template match="item[itemMapCode='1']" mode="NarrativeArray">
<Narrative>
<xsl:value-of select="./itemValue" />
</Narrative>
</xsl:template>
<xsl:template match="faRecord[recordCode='E1']" mode="InvoiceDisplayResponse">
<ItemRecord>
<xsl:apply-templates select="item" mode="InvoiceItemRecord" />
</ItemRecord>
</xsl:template>
<xsl:template match="faRecord[recordCode='E2']" mode="InvoiceDisplayResponse">
<ItemWorkCodeRecord>
<xsl:apply-templates select="item" mode="InvoiceItemWorkCodeRecord" />
<xsl:apply-templates select="key('kItemWorkCodeNarrativeArray', generate-id())" mode="InvoiceItemWorkCodeRecord"/>
</ItemWorkCodeRecord>
</xsl:template>
<!-- Invoice Item WorkCode (E2 response) Narrative Array fields -->
<xsl:template match="faRecord[recordCode='01']" mode="InvoiceItemWorkCodeRecord">
<NarrativeArray>
<xsl:apply-templates select="item" mode="ItemWorkCodeNarrativeArray" />
</NarrativeArray>
</xsl:template>
<xsl:template match="item[itemMapCode='1']" mode="ItemWorkCodeNarrativeArray">
<Narrative>
<xsl:value-of select="./itemValue" />
</Narrative>
</xsl:template>
<xsl:template match="faRecord[recordCode='E3']" mode="InvoiceDisplayResponse">
<OrderRecord>
<xsl:apply-templates select="item" mode="OrderArray" />
<xsl:apply-templates select="key('kOrderRecordApproverArray', generate-id())" mode="InvoiceOrderRecord" />
</OrderRecord>
</xsl:template>
<!-- Invoice order record (E3 response) Approver Array fields -->
<xsl:template match="faRecord[recordCode='01']" mode="InvoiceOrderRecord">
<ApproverArray>
<xsl:apply-templates select="item" mode="ApproverArray" />
</ApproverArray>
</xsl:template>
<!-- Ignore any other fields -->
<xsl:template match="*" mode="InvoiceDisplayDetail"/>
</xsl:stylesheet>
这是我的来源xml。
<?xml version="1.0" encoding="UTF-8"?>
<InvoiceDisplayRequest>
<facXML>
<faRecord>
<recordCode>E0</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>B000011</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>2000</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>2000</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E0</recordCode>
<item>
<itemMapCode>41</itemMapCode>
<itemValue>N</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>02</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>S</itemValue>
</item>
<item>
<itemMapCode>3</itemMapCode>
<itemValue>STRD</itemValue>
</item>
<item>
<itemMapCode>4</itemMapCode>
<itemValue>3080</itemValue>
</item>
<item>
<itemMapCode>5</itemMapCode>
<itemValue>0</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>03</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>narrative at invoice level here. Invoice log will only allow me to input 100 characters I believe. E</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E1</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>TFR</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E2</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>3</itemMapCode>
<itemValue>02</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E2</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>2</itemValue>
</item>
<item>
<itemMapCode>3</itemMapCode>
<itemValue>AC</itemValue>
</item>
<item>
<itemMapCode>4</itemMapCode>
<itemValue>Actor</itemValue>
</item>
<item>
<itemMapCode>5</itemMapCode>
<itemValue>5400</itemValue>
</item>
<item>
<itemMapCode>6</itemMapCode>
<itemValue>0</itemValue>
</item>
<item>
<itemMapCode>7</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>8</itemMapCode>
<itemValue>1080</itemValue>
</item>
<item>
<itemMapCode>9</itemMapCode>
<itemValue>0</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>narrative at gird level here</itemValue>
</item>
</faRecord>
</facXML>
</InvoiceDisplayRequest>
只是猜测。但基本上您应该按如下方式更改密钥:
(使用密钥修复进行更新)
<xsl:key name="kApproverDetailArray" match="faRecord[recordCode='01']"
use="generate-id(
preceding-sibling::faRecord[recordCode='E0' or recordCode='E2'][1])" />
有了它,您可以遍历以下"faRecord[recordCode='01']
faRecord[recordCode='E0' ]
:
<xsl:template match="facXML">
<xsl:for-each select="faRecord[recordCode='E0' ]">
<xsl:for-each select="key('kApproverDetailArray', generate-id()) ">
<!-- do something -->
</xsl:for-each>
</xsl:for-each>
</xsl:template>