我需要將XML傳遞給第三方系統,第三方可以理解並解析它。xml到xml轉換使用xslt
下面是我的輸入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>[email protected],[email protected],[email protected]</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>[email protected],[email protected],[email protected]</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>
的Output.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>
注:子標記(egApprovedLimitHomeCCY ....),我介紹下BO的(例如,用於CorpAppLimitDetailsBO )標籤是動態的。我不應該在xsl中使用硬編碼。請幫助我。
感謝Shil和Sean爲您提供的解決方案。兩者都適合我的要求。但我有一個更懷疑now.i中號再增加一個子標籤<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>
再次感謝。
你可能想,如果你希望有幫助的迴應 – Gratzy
感謝response.I發佈,我試圖 – Moorthy
嘿@Moorthy是代碼先試,並張貼你已經嘗試了什麼,結果你會接受一個答案? –