2013-04-24 2 views
2

저는 XSL에서 새로 왔으며 다음과 같은 경우를 찾을 수 없었습니다. source.xml을 target.xml로 변환하려고합니다. 내가 사용 모드 "그룹"을 가지고 있지만 그것은 나를 위해 작동하지 않았다 (아마 내가 그것을 제대로 사용할 수없는)xml 블록을 변환하는 방법은 무엇입니까?

source.xml :

<?xml version="1.0" encoding="ISO-8859-1"?> 

<PersonBody> 

    <Person> 
     <D>Name</D> 
     <D>Surname</D> 
     <D>Id</D> 
    </Person> 

    <PersonValues> 
     <D>Michael</D> 
     <D>Jackson</D> 
     <D>01</D> 
    </PersonValues> 

    <PersonValues> 
     <D>James</D> 
     <D>Bond</D> 
     <D>007</D> 
    </PersonValues> 

    <PersonValues> 
     <D>Kobe</D> 
     <D>Bryant</D> 
     <D>24</D> 
    </PersonValues> 

</PersonBody> 

target.xml :

<PersonBody> 
    <AllValues> 
    <Name> 
     <D>Michael</D> 
     <D>James</D> 
     <D>Kobe</D> 
    </Name> 
    <Surname> 
     <D>Jackson</D> 
     <D>Bond</D> 
     <D>Bryant</D> 
    </Surname> 
    <Id> 
     <D>01</D> 
     <D>007</D> 
     <D>24</D> 
    </Id> 
    </AllValues> 
</PersonBody> 

편집 : 출력이 변경 되었기 때문에 다른 질문을했습니다. 당신은 here

+0

는 친절하게 당신이 시도 알려 ? – siva2012

+0

''의 첫 번째 ''은 비어 있지만 그냥 오타가 무엇입니까? – JLRishe

답변

1

에서 다른 질문을 찾을 수 있습니다 이 시도 보내기 샘플 XML을 실행하면

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:key name="kColumnValue" match="PersonValues/*" 
      use="count(preceding-sibling::*)" /> 

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

    <xsl:template match="/*"> 
    <xsl:copy> 
     <AllValues> 
     <xsl:apply-templates select="Person/*" /> 
     </AllValues> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Person/*"> 
    <xsl:element name="{.}"> 
     <xsl:apply-templates select="key('kColumnValue', position() - 1)" /> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

을, 결과는 다음과 같습니다

<PersonBody> 
    <AllValues> 
    <Name> 
     <D>Michael</D> 
     <D>James</D> 
     <D>Kobe</D> 
    </Name> 
    <Surname> 
     <D>Jackson</D> 
     <D>Bond</D> 
     <D>Bryant</D> 
    </Surname> 
    <Id> 
     <D>01</D> 
     <D>007</D> 
     <D>24</D> 
    </Id> 
    </AllValues> 
</PersonBody> 
+0

그것은 작동합니다 :) 감사합니다. – dcgulse

+0

''을''으로 대체하려고합니까? –

+1

@ serhiy.h 아니요, ''은 올바른 결과를 생성하지 않습니다. ''이 올바른 결과를 내기 때문에 사용했습니다. – JLRishe

관련 문제