2010-06-10 2 views
5

<xsl:for-each> 루프의 컨텍스트에서 XML 문서의 고유 레코드 만 선택해야합니다. Visual Studio에서 XSL 1.0을 사용하도록 제한됩니다. XSLT/XPath에서 고유 레코드 선택

<availList> 
     <item> 
      <schDate>2010-06-24</schDate>    
      <schFrmTime>10:00:00</schFrmTime> 
      <schToTime>13:00:00</schToTime> 
      <variousOtherElements></variousOtherElements> 
     </item> 
     <item> 
      <schDate>2010-06-24</schDate>    
      <schFrmTime>10:00:00</schFrmTime> 
      <schToTime>13:00:00</schToTime> 
      <variousOtherElements></variousOtherElements> 
     </item> 
     <item> 
      <schDate>2010-06-25</schDate>    
      <schFrmTime>10:00:00</schFrmTime> 
      <schToTime>12:00:00</schToTime> 
      <variousOtherElements></variousOtherElements> 
     </item> 
     <item> 
      <schDate>2010-06-26</schDate>    
      <schFrmTime>13:00:00</schFrmTime> 
      <schToTime>14:00:00</schToTime> 
      <variousOtherElements></variousOtherElements> 
     </item> 
     <item> 
      <schDate>2010-06-26</schDate>    
      <schFrmTime>10:00:00</schFrmTime> 
      <schToTime>12:00:00</schToTime> 
      <variousOtherElements></variousOtherElements> 
     </item> 
    </availList> 

고유성

은 세 개의 하위 요소의 값을 기준으로해야합니다 schDate, schFrmTimeschToTime을. 두 개의 자식 요소 모두에 대해 두 개의 item 요소가 동일한 값을 갖는 경우 중복됩니다. 위의 XML에서 항목 1과 2는입니다. 나머지는 고유합니다. 위에 표시된대로 각 항목에는 비교에 포함하지 않으려는 다른 요소가 포함되어 있습니다. '독창성'은이 세 요소의 요소이어야하며 그 요소만으로 충분합니다.

나는 다음을 통해이 작업을 수행하려고했습니다 :

availList/item[not(schDate = preceding:: schDate and schFrmTime = preceding:: schFrmTime and schToTime = preceding:: schToTime)] 

이 뒤에 아이디어는 같은 schDate, schFrmTimeschToTime 아무 앞의 요소가없는 경우 레코드를 선택하는 것입니다. 그러나 출력이 이고 마지막 항목이 누락되었습니다 (). 이는 실제로 내 XPath가 전체 내에서 의 하위 요소 값이 모두 일치하는 항목을 제외하기 때문입니다. 하나의 item은 마지막 항목의 모든 하위 요소와 일치하지 않지만 각 요소의 값이 다른 항목에 개별적으로 표시되기 때문에 마지막 항목이 제외됩니다.

나는 각 앞의 항목에 대한 연결된 문자열로에 동일한 연결된 값을 모든 하위 값을 비교하여 올바른 결과를 얻을 수 있습니다. 아무도 내가 이것을 할 수있는 방법을 알고 있니?

+0

좋은 질문 (+1). XPath 및 XSLT 솔루션에 대한 내 대답을 참조하십시오. –

+1

key()를 사용하는 방법은 일반적으로 Muenchian 메서드라고합니다. http://www.jenitennison.com/xslt/grouping/muenchian.html –

답변

4

I. :

/*/item[normalize-space() and not(. = preceding-sibling::item)] 

II.보다 효율적인 (XSLT) 구현, 사용 키 : 제공된 XML 문서에 적용 할 때

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

<xsl:key name="kItemByVal" match="item" use="."/> 

<xsl:template match="/"> 
    <xsl:copy-of select= 
    "*/item[generate-id() = generate-id(key('kItemByVal', .))] 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

모두 I 및 II, 올바르게 선택/복사 다음 노드 :

<item><schDate>2010-06-24</schDate><schFrmTime>10:00:00</schFrmTime><schToTime>13:00:00</schToTime></item> 
<item><schDate>2010-06-25</schDate><schFrmTime>10:00:00</schFrmTime><schToTime>12:00:00</schToTime></item> 
<item><schDate>2010-06-26</schDate><schFrmTime>13:00:00</schFrmTime><schToTime>14:00:00</schToTime></item> 
<item><schDate>2010-06-26</schDate><schFrmTime>10:00:00</schFrmTime><schToTime>12:00:00</schToTime></item> 

업데이트 : <item>에 다른 하위 항목이있는 경우이 변환은

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

    <xsl:key name="kItemBy3Children" match="item" 
    use="concat(schDate, '+', schFrmTime, '+', schToTime)"/> 

<xsl:template match="/"> 
     <xsl:copy-of select= 
     "*/item[generate-id() 
       = generate-id(key('kItemBy3Children', 
           concat(schDate, 
             '+', schFrmTime, 
             '+', schToTime) 
           ) 
          ) 
       ] 
     "/> 
</xsl:template> 
</xsl:stylesheet> 

원하는 결과는입니다.

+0

Dimitre, 답변 해 주셔서 대단히 감사합니다. 그래도 내 경우에는 효과가 없을 것 같네요. - 제 질문을 작성할 때 명확하지 않다는 사과를드립니다 (나중에 편집했습니다). 문제는 실제로 'item'요소에는 항목의 선택 여부에 포함되지 않아야하는 여러 가지 하위 요소가 포함되어 있다는 것입니다. 나는 실제로 '진정한'고유성을 찾고있는 것이 아니라, 특정 하위 요소 값에서만 고유성을 찾고 있습니다. 나는 당신의 대답이 다른 사람들에게 가치 있다고 확신합니다. Dan –

+1

@ Daniel-I-S : 수정 된 문제에 대한 해결책으로 내 대답을 업데이트했습니다. –

+2

대단한 대답입니다. 고마워요. –

2

필자가 보았던 기술은 세 가지 주요 필드로 항목을 정렬 한 다음 앞의 모든 항목 대신 각 항목을 앞의 항목과 비교하는 두 가지 과정으로이 작업을 수행하는 것입니다.

두 개의 별도 변환을 실행하는 것이 현실적입니까? 문제를 훨씬 쉽게 만듭니다.

나는이 기술을 이전 버전 Michael Kay's XSLT book에서 보았습니다. 그의 샘플 코드에서 찾을 수 있습니다. 하나의 XPath 식과

관련 문제