我有一条输入XML消息,我试图将其与输入中的值一起转换为不同的XML。您可以查看它并指导我使用XSLT示例吗?
输入XML:
<Req_IN>
<ReqID_IN>REQIDIN001</ReqID_IN>
<ReqType_IN>AZZ002</ReqType_IN>
<VMID_IN>VMIDINV003</VMID_IN>
<PSMID_IN>PSMIDIN004</PSMID_IN>
<SIDHIQ_IN>sssiiidddd54005</SIDHIQ_IN>
<SIP_IN>SIP_IN_10.20.30.40</SIP_IN>
<MPass_IN>full details of buyer</MPass_IN>
<currency_IN>USD</currency_IN>
<amt_IN>167.78</amt_IN>
<MIDets_IN>
<itCode_IN>a</itCode_IN>
<itDesc_IN>aadesc</itDesc_IN>
<itUntprice_IN>555</itUntprice_IN>
<itQuan_IN>41</itQuan_IN>
</MIDets_IN>
<MIDets_IN>
<itCode_IN>b</itCode_IN>
<itDesc_IN>bbbdesc</itDesc_IN>
<itUntprice_IN>44343</itUntprice_IN>
<itQuan_IN>32</itQuan_IN>
</MIDets_IN>
<MIDets_IN>
<itCode_IN>c</itCode_IN>
<itDesc_IN>abcdesc</itDesc_IN>
<itUntprice_IN>8979</itUntprice_IN>
<itQuan_IN>32</itQuan_IN>
</MIDets_IN>
<ConBDetails_IN>
<cname_IN>firstname lastname</cname_IN>
<ConAddress1>ConBDetails addressline1 </ConAddress1>
<ConAddress2>ConBDetails addressline2</ConAddress2>
<ConAddress3>ConBDetails addressline3</ConAddress3>
<City_IN>ConBdetails_city</City_IN>
<pcode_IN>ConBdetails_PIN</pcode_IN>
<c_IN>ConBdetails US</c_IN>
</ConBDetails_IN>
<mShipDets_IN>
<FullName>fname lname</FullName>
<MerAddress1>mShip addressline1</MerAddress1>
<MerAddress2>mShip addressline2</MerAddress2>
<MerAddress3>mShip addressline3</MerAddress3>
<City_IN>mShip city</City_IN>
<pcode_IN>mShip PIN</pcode_IN>
<c_IN>mSHIP US</c_IN>
</mShipDets_IN>
<VmeTransactionIdentifier>
<VMeUniqueId> a9001</VMeUniqueId>
<AuthenticationMethod>01</AuthenticationMethod>
<AuthenticationReasonCode>1C</AuthenticationReasonCode>
</VmeTransactionIdentifier>
</Req_IN>
输出XML:
<Req_IN_new>
<PSMID_IN_new>value of PSMID_IN element(PSMIDIN004)</PSMID_IN_new>
<ReqID_IN_new>value of ReqID_IN element (REQIDIN001)</ReqID_IN_new>
<SPT_new>value of ReqID_IN element (REQIDIN001)</SPT_new>
<ConBDetails_IN_new>
<cname_IN_new>firstname lastname</cname_IN_new>
<street1>ConBDetails addressline1 </street1>
<street2>ConBDetails addressline2 </street2>
<street3>ConBDetails addressline3</street3>
<city>ConBdetails_city</city>
<postalCode>ConBdetails_PIN</postalCode>
<country>ConBdetails_US</country>
<ipAddress>value of SIP_IN element (SIP_IN_10.20.30.40)</ipAddress>
</ConBDetails_IN_new>
<MPass_IN_new>full details of buyer </MPass_IN_new>
<mShipDets_IN_new>
<FullName_new>fname lname</FullName_new>
<MerAddress1_new>mShip addressline1</MerAddress1_new>
<MerAddress2_new>mShip addressline2</MerAddress2_new>
<MerAddress3_new>mShip addressline3</MerAddress3_new>
<City_IN_new>mShip city</City_IN_new>
<pcode_IN_new>mShip PIN</pcode_IN_new>
<c_IN_new>mSHIP US</c_IN_new>
</mShipDets_IN_new>
<MIDets_IN_new id="0">
<itCode_IN_new>a</itCode_IN_new>
<itDesc_IN_new>aadesc</itDesc_IN_new>
<itUntprice_IN_new>555</itUntprice_IN_new>
<itQuan_IN_new>41</itQuan_IN_new>
</MIDets_IN_new>
<MIDets_IN_new id="1">
<itCode_IN_new>b</itCode_IN_new>
<itDesc_IN_new>bbbdesc</itDesc_IN_new>
<itUntprice_IN_new>44343</itUntprice_IN_new>
<itQuan_IN_new>32</itQuan_IN_new>
</MIDets_IN_new>
<MIDets_IN_new id="2">
<itCode_IN_new>c</itCode_IN_new>
<itDesc_IN_new>abcdesc</itDesc_IN_new>
<itUntprice_IN_new>8979</itUntprice_IN_new>
<itQuan_IN_new>41</itQuan_IN_new>
</MIDets_IN_new>
<totalAmt>
<amt_IN_new>167.78</amt_IN_new>
<currency_IN_new>USD</currency_IN_new>
</totalAmt>
</Req_IN_new>
答案 0 :(得分:0)
您需要使用xsl:template
,xsl_value-of
和xsl:apply-templates
等XSL元素迭代XML元素。我不是为你做的,但这是一个典型的片段:
<xsl:template match="ConAddress1">
<street1><xsl:value-of select="."/></street1>
</xsl:template>
在这里,您要转换一个元素名称,选择其内容并创建新元素(street1
)。您的XSL可以包含大约30个模板。
如果你看任何教程,他们会有很多这种转换的例子。
答案 1 :(得分:0)
这种转变可以满足您的要求。基本上,根元素的模板按照它们在输出中出现的顺序从源中选取元素,然后通用模板复制每个元素,并在名称中添加_new
。
许多其他模板处理那些与其他元素处理不同的元素。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/Req_IN">
<Req_IN_new>
<xsl:apply-templates select="PSMID_IN"/>
<xsl:apply-templates select="ReqID_IN"/>
<xsl:apply-templates select="ConBDetails_IN"/>
<xsl:apply-templates select="MPass_IN"/>
<xsl:apply-templates select="mShipDets_IN"/>
<xsl:apply-templates select="MIDets_IN"/>
<totalAmt>
<amt_IN_new>
<xsl:value-of select="amt_IN"/>
</amt_IN_new>
<currency_IN_new>
<xsl:value-of select="currency_IN"/>
</currency_IN_new>
</totalAmt>
</Req_IN_new>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{concat(name(), '_new')}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ReqID_IN">
<xsl:element name="{concat(name(), '_new')}">
<xsl:apply-templates/>
</xsl:element>
<SPT_new>
<xsl:apply-templates/>
</SPT_new>
</xsl:template>
<xsl:template match="ConBDetails_IN">
<xsl:element name="{concat(name(), '_new')}">
<xsl:apply-templates/>
<ipAddress>
<xsl:value-of select="preceding-sibling::SIP_IN"/>
</ipAddress>
</xsl:element>
</xsl:template>
<xsl:template match="ConBDetails_IN/*[starts-with(name(),'ConAddress')]">
<xsl:element name="{concat('street', substring-after(name(), 'ConAddress'))}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ConBDetails_IN/City_IN">
<city>
<xsl:apply-templates/>
</city>
</xsl:template>
<xsl:template match="ConBDetails_IN/pcode_IN">
<postalCode>
<xsl:apply-templates/>
</postalCode>
</xsl:template>
<xsl:template match="ConBDetails_IN/c_IN">
<country>
<xsl:apply-templates></xsl:apply-templates>
</country>
</xsl:template>
<xsl:template match="MIDets_IN">
<xsl:element name="{concat(name(), '_new')}">
<xsl:attribute name="id">
<xsl:value-of select="position()-1"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<Req_IN_new>
<PSMID_IN_new>PSMIDIN004</PSMID_IN_new>
<ReqID_IN_new>REQIDIN001</ReqID_IN_new>
<SPT_new>REQIDIN001</SPT_new>
<ConBDetails_IN_new>
<cname_IN_new>firstname lastname</cname_IN_new>
<street1>ConBDetails addressline1 </street1>
<street2>ConBDetails addressline2</street2>
<street3>ConBDetails addressline3</street3>
<city>ConBdetails_city</city>
<postalCode>ConBdetails_PIN</postalCode>
<country>ConBdetails US</country>
<ipAddress>SIP_IN_10.20.30.40</ipAddress>
</ConBDetails_IN_new>
<MPass_IN_new>full details of buyer</MPass_IN_new>
<mShipDets_IN_new>
<FullName_new>fname lname</FullName_new>
<MerAddress1_new>mShip addressline1</MerAddress1_new>
<MerAddress2_new>mShip addressline2</MerAddress2_new>
<MerAddress3_new>mShip addressline3</MerAddress3_new>
<City_IN_new>mShip city</City_IN_new>
<pcode_IN_new>mShip PIN</pcode_IN_new>
<c_IN_new>mSHIP US</c_IN_new>
</mShipDets_IN_new>
<MIDets_IN_new id="0">
<itCode_IN_new>a</itCode_IN_new>
<itDesc_IN_new>aadesc</itDesc_IN_new>
<itUntprice_IN_new>555</itUntprice_IN_new>
<itQuan_IN_new>41</itQuan_IN_new>
</MIDets_IN_new>
<MIDets_IN_new id="1">
<itCode_IN_new>b</itCode_IN_new>
<itDesc_IN_new>bbbdesc</itDesc_IN_new>
<itUntprice_IN_new>44343</itUntprice_IN_new>
<itQuan_IN_new>32</itQuan_IN_new>
</MIDets_IN_new>
<MIDets_IN_new id="2">
<itCode_IN_new>c</itCode_IN_new>
<itDesc_IN_new>abcdesc</itDesc_IN_new>
<itUntprice_IN_new>8979</itUntprice_IN_new>
<itQuan_IN_new>32</itQuan_IN_new>
</MIDets_IN_new>
<totalAmt>
<amt_IN_new>167.78</amt_IN_new>
<currency_IN_new>USD</currency_IN_new>
</totalAmt>
</Req_IN_new>