2013-03-02 4 views
1

일부 요소를 그룹화하고 새 요소 아래에 함께 가져와야합니다.xslt의 새 노드 아래에 요소 그룹화

다음은 샘플 레코드입니다. 주소 정보를 추가 레이어로 그룹화하고 싶습니다.

여기에 원래의 기록이다 -

<Records> 
    <People> 
    <FirstName>John</FirstName> 
    <LastName>Doe</LastName> 
    <Middlename /> 
    <Age>20</Age> 
    <Smoker>Yes</Smoker> 
    <Address1>11 eleven st</Address1> 
    <Address2>app 11</Address2> 
    <City>New York</City> 
    <State>New York</State> 
    <Status>A</Status> 
    </People> 
    </Records> 

예상 결과 : I과 같은 새로운 요소 아래에 그룹 주소 데이터에 필요 -

<Records> 
    <People> 
    <FirstName>John</FirstName> 
    <LastName>Doe</LastName> 
    <Middlename /> 
    <Age>20</Age> 
    <Smoker>Yes</Smoker> 
    <Address>  
     <Address1>11 eleven st</address1> 
     <Address2>app 11</address2> 
     <City>New York</City> 
     <State>New York</State> 
    </Address>  
    <Status>A</Status> 
    </People> 
    </Records> 

어떤 도움도 좋을 것! 당신은

답변

1

이 그것을해야 감사합니다 : 당신의 입력 XML에서 실행

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

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

    <xsl:template match="People"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()[not(self::Address1 or 
                self::Address2 or 
                self::City or 
                self::State)]" /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Smoker"> 
    <xsl:call-template name="copy" /> 
    <Address> 
     <xsl:apply-templates select="../Address1 | 
            ../Address2 | 
            ../City | 
            ../State" /> 
    </Address> 
    </xsl:template> 
</xsl:stylesheet> 

,이 생산 :

<Records> 
    <People> 
    <FirstName>John</FirstName> 
    <LastName>Doe</LastName> 
    <Middlename /> 
    <Age>20</Age> 
    <Smoker>Yes</Smoker> 
    <Address> 
     <Address1>11 eleven st</Address1> 
     <Address2>app 11</Address2> 
     <City>New York</City> 
     <State>New York</State> 
    </Address> 
    <Status>A</Status> 
    </People> 
</Records> 
관련 문제