2013-05-19 3 views
0

입력에서 가져온 값과 함께 다른 XML로 변환하려는 입력 XML 메시지가 있습니다. 그것을 살펴보고 샘플 XSLT를 안내해 주시겠습니까?XSLT를 사용하여 새 XML 작성

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

이 변환은 사용자가 요청한 것을 수행합니다. 근본적으로 루트 요소의 템플릿은 원본에서 요소를 출력에 나타나는 순서대로 선택하고 범용 템플릿은 각 요소를 복사하고 이름에 _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> 
+0

보로딘, 귀하의 빠른 응답 주셔서 감사합니다. xslt를 테스트하고 완벽하게 작동합니다. 출력 요소 xml 요소를 입력 요소와 독립적으로 만들 수 있습니까? 출력 XML 요소가 '_new'접미사가있는 입력 XML에서 파생되지 않아야합니다. 이 출력 XML 요소는 출력 XML 당 fullname을 제공하여 구성해야합니다. – sarma

+0

Borodin, 도와 주셔서 감사합니다, 귀하의 xslt 성공적으로 일했다. – sarma

0

당신은 XSL 요소를 사용하여 XML 요소를 반복해야 같은 xsl:template, xsl_value-ofxsl:apply-templates. 나는 당신을 위해 그것을 할 수 없다 그러나 여기 전형적인 조각입니다 : 여기

<xsl:template match="ConAddress1"> 
    <street1><xsl:value-of select="."/></street1> 
</xsl:template> 

당신은 하나의 요소 이름으로 변환 내용을 선택하고 새로운 요소 (street1)를 만드는이. XSL에는 약 30 개의 템플릿이 포함될 수 있습니다.

자습서를 보면이 유형의 변환에 대한 많은 예제가 있습니다.