xslt 1.0。我需要在以下表达式中选择几个节点并排除其他节点

时间:2016-03-18 15:10:57

标签: xslt xslt-1.0

我需要仅包含来自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>

1 个答案:

答案 0 :(得分:1)

只猜测。但基本上你应该改变密钥如下:
(使用密钥修复更新)

<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>