xslt 1.0转换结构问题

时间:2016-02-10 16:11:43

标签: xml xslt xslt-1.0

我想要的结果是:

对于与//root/document/ins/@contractId匹配的每个//root/document/DetailGroup/Detail/Order/contract/@contractIdDetailSubtype为毛,order type=Surrender

应该总结交易信息。需要总结的金额应该只有full_surpartial_sur,其他所有内容都应该被忽略

他们想要一个特定的结构,每个匹配逻辑一次。

首先需要转换的xml文件:

    <Document>
        <ins contractId="670">
          <Role>
             <person personId="60"/>
             <role>INS</role>
          </Role>
        </ins>
        <ins contractId="477">
          <Role>
             <person personId="60"/>
             <role>OWN</role>
          </Role>
        </ins>
       <DetailGroup>
            <Detail type="detailgross" DetailId="25">
                 <DetailSubtype>GROSS</DetailSubtype>
                 <Order type="Surrender">
                    <contract contractId="477"/>
                    <Transaction>
                       <Transaction>
                          <completedAmount>1443.08</completedAmount>
                          <TransactionSubtype>yield</TransactionSubtype>
                       </Transaction>
                      <Transaction>
                          <completedAmount>100.00</completedAmount>
                          <TransactionSubtype>fee</TransactionSubtype>
                       </Transaction>
                       <Transaction>
                          <completedAmount>200.00</completedAmount>
                          <TransactionSubtype>full_sur</TransactionSubtype>
                       </Transaction>
                    </Transaction>
                 </Order>
            </Detail>       

            <Detail type="detailgross" DetailId="24">
                 <DetailSubtype>gross</DetailSubtype>
                 <Order type="Surrender">
                    <contract contractId="670"/>
                    <Transaction>
                       <Transaction>
                          <completedAmount>1443.08</completedAmount>
                          <TransactionSubtype>fee</TransactionSubtype>
                       </Transaction>
                       <Transaction>
                          <completedAmount>100.00</completedAmount>
                          <TransactionSubtype>full_sur</TransactionSubtype>
                       </Transaction>
                       <Transaction>
                          <completedAmount>1000.00</completedAmount>
                          <TransactionSubtype>partial_sur</TransactionSubtype>
                       </Transaction>
                    </Transaction>
                 </Order>
            </Detail>

    </DetailGroup>
    </Document>

然后他们需要怎么看:

    <root>
        <document>
            <person role="INS" personId="60">
            </person>
            <bet>
                <spar>
                    <at sum="1100"/>
                </spar>
            </bet>
        </document>
        <document>
            <person role="OWN" personId="60">
            </person>
            <bet>
                <spar>
                    <at sum="200"/>
                </spar>
            </bet>
        </document>
    </root>

编辑:我只能使用xslt 1.0

<xsl:template name="example">
<xsl:variable name="detailId" select="//Document/DetailGrooup/Detail/Order/contract"/>
<xsl:for-each select="//Document/ins[@contractId=$detailId/@contractId]">
<document>
<person>
<xsl:attribute name="Role">
<xsl:value-of select="//Document/Role"/>
</xsl:attribute>
<xsl:attribute name="personId">
<xsl:value-of select="//Document/Role/Person/@PersonId"/>
</xsl:attribute>

</person>
<bet>
<spar>
<xsl:if test=".././DetailGrooup/Detail[DetailSubtype='GROSS' and ./Order/@type='Surrender' and /Order/Transaction/Transaction/TransactionSubtype='full_sur']">
<xsl:attribute name="at">
<xsl:value-of select="sum(//Document/DetailGrooup/Detail/Order[/contract/@contractId='@contractId']/Transaction/Transaction/completedAmount)"
</xsl:attribute>
</xsl:if>
</spar>
</bet>

</document>
</xsl:for-each>


</xsl:template>

1 个答案:

答案 0 :(得分:1)

尝试这个

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <!-- use a key to match contractId attributes -->
    <xsl:key name="kContract" match="Order" use="contract/@contractId"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Document">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>

    <xsl:template match="ins">
        <document>
            <person role="{descendant::role}" personId="{descendant::person/@personId}"/>
            <bet>
                <spar>
                    <at>
                        <xsl:attribute name="sum">
                            <xsl:value-of select="sum(key('kContract', @contractId)/descendant::completedAmount[parent::Transaction[TransactionSubtype = 'full_sur' or TransactionSubtype = 'partial_sur']])"/>
                        </xsl:attribute>
                    </at>
                </spar>
            </bet>
        </document>
    </xsl:template>

    <xsl:template match="DetailGroup"/>

</xsl:stylesheet>