简体   繁体   English

xslt 1.0转换结构问题

[英]xslt 1.0 transformation structure issue

The result I want is: 我想要的结果是:

For each //root/document/ins/@contractId that has a match with //root/document/DetailGroup/Detail/Order/contract/@contractId , and the DetailSubtype is gross, order type=Surrender . 对于与//root/document/DetailGroup/Detail/Order/contract/@contractId匹配的每个//root/document/ins/@contractId //root/document/DetailGroup/Detail/Order/contract/@contractId ,并且DetailSubtype为gross, order type=Surrender

The transaction information should be summed up. 交易信息应该汇总。 The amount needed to be summed up should ONLY be full_sur and partial_sur , all else is to be ignored 需要加总的数量应full_surpartial_sur ,所有其他都将被忽略

And they want a specific structure, once for each matching logic. 他们想要一个特定的结构,每个匹配逻辑一次。

First the xml file that needs to be transformed: 首先,需要转换的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>

And then how they need it to look: 然后他们如何看待它:

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

EDIT: I can only use xslt 1.0 编辑:我只能使用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>

try this one 试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM