1

我需要将 XML 传递给第三方系统,第三方系统可以理解并解析它。

下面是我通过从数据库中获取数据来创建数据的输入 xml。

<FIXML>
    <Header>
        <RequestID>ReqID8942</RequestID>
        <RequestType>DocGen</RequestType>
        <Version>10.6</Version>
        <BankId>01</BankId>
        <ChannelId>LOS</ChannelId>
    </Header>
    <Body>
        <Data>
            **<CorpAppLimitDetailsBO>
            <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
            <ApprovedLimitCCY>INR</ApprovedLimitCCY>
            <ApprovedLimit>100.0</ApprovedLimit>
            <LimitClassification>ROOT</LimitClassification>
        </CorpAppLimitDetailsBO>
        <CorpAppLimitDetailsBO>
            <ApprovedLimitHomeCCY>0.0</ApprovedLimitHomeCCY>
            <ApprovedLimitCCY/>
            <ApprovedLimit>500.0</ApprovedLimit>
            <LimitClassification>CLASSIFICATION1</LimitClassification>
        </CorpAppLimitDetailsBO>
        <CorpAppLimitDetailsBO>
            <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
            <ApprovedLimitCCY>INR</ApprovedLimitCCY>
            <ApprovedLimit>100.0</ApprovedLimit>
            <LimitClassification>CLASSIFICATION1</LimitClassification>
        </CorpAppLimitDetailsBO>
        <CorpAppProductDetailsBO>
            <ProductCategory>3</ProductCategory>
        </CorpAppProductDetailsBO>
        <CorpAppProductDetailsBO>
            <ProductCategory>1</ProductCategory>
        </CorpAppProductDetailsBO>
        <CorpAppProductDetailsBO>
            <ProductCategory>2</ProductCategory>
            </CorpAppProductDetailsBO>**
            <TemplateDetails>
                <Template>tempid001</Template>
            </TemplateDetails>
            <SelectedClauses>
                <Clauses>
                    <Clause>clause1</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause2</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause3</Clause>
                </Clauses>
            </SelectedClauses>
            <Distribution>
                <Email>email1@domain.com,email2@domain.com,email3@domain.com</Email>
                <Print>blrkec3030,blrkec3031</Print>
            </Distribution>
        </Data>
    </Body>
</FIXML>

我想使用 XSLT 将此输入 XML 转换为另一种 XML 格式。

以下是我需要的格式,

<FIXML>
    <Header>
        <RequestID>ReqID8942</RequestID>
        <RequestType>DocGen</RequestType>
        <Version>10.6</Version>
        <BankId>01</BankId>
        <ChannelId>LOS</ChannelId>
    </Header>
    <Body>
        <Data>
            **<LimitDetails>
            <Limit>
                <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
                <ApprovedLimitCCY>INR</ApprovedLimitCCY>
                <ApprovedLimit>100.0</ApprovedLimit>
                <LimitClassification>ROOT</LimitClassification>
            </Limit>
            <Limit>
                <ApprovedLimitHomeCCY>0.0</ApprovedLimitHomeCCY>
                <ApprovedLimitCCY/>
                <ApprovedLimit>500.0</ApprovedLimit>
                <LimitClassification>CLASSIFICATION1</LimitClassification>
            </Limit>
            <Limit>
                <ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
                <ApprovedLimitCCY>INR</ApprovedLimitCCY>
                <ApprovedLimit>100.0</ApprovedLimit>
                <LimitClassification>CLASSIFICATION1</LimitClassification>
            </Limit>
        </LimitDetails>
        <ProductDetails>
            <Product>
                <ProductCategory>3</ProductCategory>
            </Product>
            <Product>
                <ProductCategory>1</ProductCategory>
            </Product>
            <Product>
                <ProductCategory>2</ProductCategory>
            </Product>
            </ProductDetails>**
            <TemplateDetails>
                <Template>tempid001</Template>
            </TemplateDetails>
            <SelectedClauses>
                <Clauses>
                    <Clause>clause1</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause2</Clause>
                </Clauses>
                <Clauses>
                    <Clause>clause3</Clause>
                </Clauses>
            </SelectedClauses>
            <Distribution>
                <Email>email1@domain.com,email2@domain.com,email3@domain.com</Email>
                <Print>blrkec3030,blrkec3031</Print>
            </Distribution>
        </Data>
    </Body>
</FIXML>

请帮帮我,因为我必须在另外 2 天的时间内完成任务。

我尝试使用下面的代码,但我得到了低于输出的结果,但其余的标签,例如<FIXML> , <TemplateDetails>等,并没有作为输出 xml 的一部分出现。

xsl代码如下:

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

    <xsl:output indent="yes" />    <!-- This identity template copies the document -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>

    <!--
        This template will only match the 'CorpAppLimitDetailsBO' 
        nodes and modify them the way you want.
    -->

<xsl:template match="/*">
        <xsl:element name="LimitDetails">
            <xsl:for-each select="//CorpAppLimitDetailsBO">
                <xsl:element name="Limit">
                    <xsl:for-each select="*">
                        <xsl:copy-of select="." />
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
        <xsl:element name="ProductDetails">
            <xsl:for-each select="//CorpAppProductDetailsBO">
                <xsl:element name="Product">
                    <xsl:for-each select="*">
                        <xsl:copy-of select="." />
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>



</xsl:stylesheet>

下面的输出.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LimitDetails>
<Limit>
<ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
<ApprovedLimitCCY>INR</ApprovedLimitCCY>
<ApprovedLimit>100.0</ApprovedLimit>
<LimitClassification>ROOT</LimitClassification>
</Limit>
<Limit>
<ApprovedLimitHomeCCY>0.0</ApprovedLimitHomeCCY>
<ApprovedLimitCCY/>
<ApprovedLimit>500.0</ApprovedLimit>
<LimitClassification>CLASSIFICATION1</LimitClassification>
</Limit>
<Limit>
<ApprovedLimitHomeCCY>100.0</ApprovedLimitHomeCCY>
<ApprovedLimitCCY>INR</ApprovedLimitCCY>
<ApprovedLimit>100.0</ApprovedLimit>
<LimitClassification>CLASSIFICATION1</LimitClassification>
</Limit>
</LimitDetails>
<ProductDetails>
<Product>
<ProductCategory>3</ProductCategory>
</Product>
<Product>
<ProductCategory>1</ProductCategory>
</Product>
<Product>
<ProductCategory>2</ProductCategory>
</Product>
</ProductDetails>

注意:我在 BO(例如 CorpAppLimitDetailsBO)标签下显示的子标签(例如,egApprovedLimitHomeCCY....)是动态的。我不应该在 xsl 中硬编码。我是 XSLT 的新手。请帮帮我。

感谢 Shil 和 Sean 的解决方案。两者都非常适合我的要求。但我现在还有一个疑问。我在下面添加了一个子<DBApplicantMiscDetails>标签

<CorpAppProductDetailsBO>

输入xml:

   <CorpAppProductDetailsBO>
    <ProductCategory>2</ProductCategory>
        <DBApplicantMiscDetails>
            <APPLICANTMISCID>400000</APPLICANTMISCID>
            <APPLICANTID>400030</APPLICANTID>
            <MISCTYPE>APPLIED</MISCTYPE>
        </DBApplicantMiscDetails>
   </CorpAppProductDetailsBO>

以下是我期望的输出格式。

<ProductDetails>
<Product>
 <ProductCategory>2</ProductCategory>
 <APPLICANTMISCID>400000</APPLICANTMISCID>
 <APPLICANTID>400030</APPLICANTID>
 <MISCTYPE>APPLIED</MISCTYPE>
</Product>
</ProductDetails>

再次感谢。

4

3 回答 3

0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:key name="kDetails" match="*
   [starts-with(name(),'CorpApp') and
    substring(name(), string-length(name()) - 8) = 'DetailsBO']"
         use="substring-before(name(),'DetailsBO')" />

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

<xsl:template match="*[*[key('kDetails',substring-before(name(),'DetailsBO'))]]">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="*[generate-id() =
    generate-id(key('kDetails',
     substring-before(name(),'DetailsBO'))[1])]" mode="group" />
    <xsl:apply-templates select="*[not(
       key('kDetails',substring-before(name(),'DetailsBO')))]
       |comment()|processing-instruction()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="group">
  <xsl:variable name="group-name" select="substring-after(substring-before(name(),'DetailsBO'),'CorpApp')" />
  <xsl:element name="{$group-name}Details">
   <xsl:for-each select="key('kDetails',substring-before(name(),'DetailsBO'))">
     <xsl:element name="{$group-name}">
       <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
   </xsl:for-each>  
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
于 2012-10-08T15:49:10.513 回答
0
<xsl:stylesheet version="2.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="/">
        <xsl:apply-templates select="FIXML"/>
    </xsl:template>
    <xsl:template match="FIXML">
        <FIXML>
            <xsl:apply-templates select="Header"/>
            <xsl:apply-templates select="Body"/>
        </FIXML>
    </xsl:template>
    <xsl:template match="Header">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Body">
        <Body>
            <xsl:apply-templates select="Data"/>
        </Body>
    </xsl:template>
    <xsl:template match="Data">
        <Data>
            <LimitDetails>
                <xsl:apply-templates select="CorpAppLimitDetailsBO"/>
            </LimitDetails>
            <ProductDetails>
                <xsl:apply-templates select="CorpAppProductDetailsBO"/>
            </ProductDetails>
            <xsl:apply-templates select="TemplateDetails"/>
            <xsl:apply-templates select="SelectedClauses"/>
            <xsl:apply-templates select="Distribution"/>
        </Data>
    </xsl:template>
    <xsl:template match="CorpAppLimitDetailsBO">
        <Limit>
            <xsl:copy-of select="child::*"/>
        </Limit>
    </xsl:template>
    <xsl:template match="CorpAppProductDetailsBO">
        <xsl:apply-templates select="ProductCategory"/>
    </xsl:template>
    <xsl:template match="ProductCategory">
        <Product>
            <xsl:copy-of select="."/>
        </Product>
    </xsl:template>
    <xsl:template match="TemplateDetails">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="SelectedClauses">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Distribution">
    <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
于 2012-10-08T15:53:30.217 回答
0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="FIXML"/>
    </xsl:template>
    <xsl:template match="FIXML">
        <FIXML>
            <xsl:apply-templates select="Header"/>
            <xsl:apply-templates select="Body"/>
        </FIXML>
    </xsl:template>
    <xsl:template match="Header">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Body">
        <Body>
            <xsl:apply-templates select="Data"/>
        </Body>
    </xsl:template>
    <xsl:template match="Data">
        <Data>
            <LimitDetails>
                <xsl:apply-templates select="CorpAppLimitDetailsBO"/>
            </LimitDetails>
            <ProductDetails>
                <xsl:apply-templates select="CorpAppProductDetailsBO"/>
            </ProductDetails>
            <xsl:apply-templates select="TemplateDetails"/>
            <xsl:apply-templates select="SelectedClauses"/>
            <xsl:apply-templates select="Distribution"/>
        </Data>
    </xsl:template>
    <xsl:template match="CorpAppLimitDetailsBO">
        <Limit>
            <xsl:copy-of select="child::*"/>
        </Limit>
    </xsl:template>
    <xsl:template match="CorpAppProductDetailsBO">
    <Product>
        <xsl:apply-templates select="ProductCategory"/>
         <xsl:apply-templates select="DBApplicantMiscDetails"/>
    </Product>
    </xsl:template>
    <xsl:template match="ProductCategory">
            <xsl:copy-of select="."/>
    </xsl:template>
     <xsl:template match="DBApplicantMiscDetails">
    <xsl:copy-of select="child::*"/>
    </xsl:template>
    <xsl:template match="TemplateDetails">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="SelectedClauses">
    <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Distribution">
    <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
于 2012-10-10T14:29:26.830 回答