2012-02-06 3 views
0

<leg>은 여행 레그를 기록합니다. 출장은 문서 순서로 기록됩니다. 일부 다리는 같은 장소 (예 : MA에서 MA까지)에서 시작하고 끝나고, 일부 다리는 (예 : MA에서 CT까지) 장소로 이동합니다. 시작 및 종료 위치에 대한 일부 데이터가 누락되었습니다.속성의 데이터 순서 지정 및 데이터 딕셔닝

<trip> 
    <place>MA</place> 
    <place>CT</place> 
    <place>NY</place> 
    <place>DE</place> 
    <place>NY</place> 
    <place>PA</place> 
</trip> 

편집 :

OK, 나는 어떻게 볼 생각 순서가 방문에 XSLT/XPATH 1.0을 사용하여

<trip> 
    <leg start="MA" stop="MA" /> 
    <leg start="MA" stop="CT" /> 
    <leg start="NY"   /> 
    <leg      /> 
    <leg start="DE"   /> 
    <leg   stop="DE" /> 
    <leg start="NY" stop="PA" /> 
</trip> 

, 나는, 장소의 목록을 원하는 it :

<xsl:template match="leg" mode="tour"> 
    <xsl:if test="string(@start)"> 
     <place><xsl:value-of select="@start"></place> 
    </xsl:if> 
    <xsl:if test="string(@end)"> 
     <place><xsl:value-of select="@end"></place> 
    </xsl:if> 
</xsl:template> 


<xsl:variable name="rtfPlaces"> 
    <xsl:apply-templates select="trip/leg" mode="tour" /> 
</xsl:variable> 

<xsl:variable name="places" select="exslt:node-set($rtfPlaces)" /> 

<xsl:variable name="uniquePlaces" select="$places/place[1] | $places/place[.!=preceding-sibling::place[1]]" /> 

해결책 : 실제로 두 단계로 처리해야합니까? 따로 따로 작성하여 @start와 @end를 필요한 순서로 얻으십시오. 그런 다음 두 번째 패스에서 고유 한 값을 선택하십시오.

출력에 trip 요소가 표시되지 않지만 실제로 필요하지는 않습니다.

노드 집합 확장이 필요하지만 괜찮습니다.

모두 한 번에 할 수 있다면 어떻게되는지 모르겠다. 이것은 한 번에 그것을 얻을해야

+0

어떻게 좋은 ... 당신이 묻고 싶은 질문이 있습니까? 이것은 "나를 위해이 코드 작성"사이트가 아니라 "여기 내가 시도한 것, 그게 잘못된 것인가?"입니다. –

+0

나는 내 개인 배플을 통해 사람을 데려 가지 않았을 때보 다 일반적으로 더 친절한 대답을 보냈습니다. 범죄에 대한 사과. 이것에 관해, 나는 어디에서 시작해야하는지 명확하지 않다. 내가 serialize하고 de-dup을한다고 생각했지만 속성이 문서 순서로 처리되지 않았기 때문에 시작과 중단을 순서대로 어떻게 인터리브합니까? 머리를 긁적이면서 처음에는 직렬화조차도 올바른 전략이었습니다. 난 방황하고, 누군가가 잃어버린 영혼에 도움을 줄 경우를 체크하고, 코드 오류가 발생하면 게시물을 편집합니다. – JPM

답변

1

...

XML 입력

<trip> 
    <leg start="MA" stop="MA" /> 
    <leg start="MA" stop="CT" /> 
    <leg start="NY"   /> 
    <leg      /> 
    <leg start="DE"   /> 
    <leg   stop="DE" /> 
    <leg start="NY" stop="PA" /> 
</trip> 

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="trip"> 
    <trip> 
     <xsl:apply-templates/> 
    </trip> 
    </xsl:template> 

    <!--This is so we don't get duplicate entries 
    for both attributes when they are the same.--> 
    <xsl:template match="@stop[.=parent::*/@start]"/> 

    <xsl:template match="@start|@stop"> 
    <xsl:if test="not(parent::leg/preceding-sibling::leg[1][@start = current() or @end = current()])"> 
     <place><xsl:value-of select="."/></place>  
    </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

XML 출력

<trip> 
    <place>MA</place> 
    <place>CT</place> 
    <place>NY</place> 
    <place>DE</place> 
    <place>NY</place> 
    <place>PA</place> 
</trip> 
또한이 템플릿은 @start@stop 전에 처리하기 것을 100 % 확인하기 위해 추가 할 수

:

<xsl:template match="leg"> 
    <xsl:apply-templates select="@start"/> 
    <xsl:apply-templates select="@stop"/> 
    </xsl:template> 
+0

훨씬 좋습니다. 감사! 내 XSLT 학습에서 start = stop을 다루기 위해 XSLT 우선 순위 지정 기 사용을 고려하지 않았습니다. 나를위한 좋은 교훈. +1, 질문에 답변했습니다. (원래 Q를 더듬는 마음이 있다면 부탁드립니다.) – JPM

+0

대단히 반갑습니다. +1 –

관련 문제