2017-12-20 10 views
-2

입력 XMLXSLT 고유 레코드를 가져 오는 제품

<Products> 
    <Product> 
     <ProductDetail> 
      <ProductTitle>title1</ProductTitle> 
     </ProductDetail> 
     <Contact> 
      <ContactHome>adad</ContactHome> 
      <ContactInfo> 
       <ContactNum>1</ContactNum> 
       <ContactSINum>2</ContactSINum> 
      </ContactInfo> 
     </Contact> 
    </Product> 
    <Product> 
     <ProductDetail> 
      <ProductTitle>title2</ProductTitle> 
     </ProductDetail> 
     <Contact> 
      <ContactHome/> 
      <ContactInfo> 
       <ContactNum>3</ContactNum> 
       <ContactSINum>4</ContactSINum> 
      </ContactInfo> 
     </Contact> 
    </Product> 
    <Product> 
     <ProductDetail> 
      <ProductTitle>title1</ProductTitle> 
     </ProductDetail> 
     <Contact> 
      <ContactHome/> 
      <ContactInfo> 
       <ContactNum>1</ContactNum> 
       <ContactSINum>2</ContactSINum> 
      </ContactInfo> 
     </Contact> 
    </Product> 
</Products> 

출력 XML

<Productcoll> 
    <ProductINFO> 
     <ProductTitle>title1</ProductTitle> 
     <ContactNum>1</ContactNum> 
     <ContactSINum>2</ContactSINum> 
    </ProductINFO> 
    <ProductINFO> 
     <ProductTitle>title2</ProductTitle> 
     <ContactNum>3</ContactNum> 
     <ContactSINum>4</ContactSINum> 
    </ProductINFO> 
</Productcoll> 

키 사용하고 ID를 생성하여 XSLT로했습니다. 하지만 .... 키를 다른 지점의 노드를 사용하여 사용하는 방법? 공유 된 출력에 기초

+0

논리는 무엇입니까? 'ProductTitle'별로 그룹화하려고합니까? 일부 요소가 제외되는 이유는 무엇입니까 (예 :'ContactHome')? 두 요소가 같은 이름이지만 다른 값을 갖는다면 어떻게 될까요? (같은 'ProductTitle'에 대해 ' 1'및 ''과 같이) 시도한 XSLT로 질문을 업데이트 할 수 있습니까? –

+0

중복 텍스트를 제거하기 위해 시도한 코드를 추가하고 질문을 편집하십시오. "주로 코드"라는 경고는 이유가 있기 위해 설정됩니다. 수표를 속이려면 덤프 텍스트를 사용하지 마십시오. 정교한 진술로 질문을 명확히하십시오. –

답변

0

, 은이 그룹화 <ProductTitle>, <ContactNum><ContactSINum> 만 그 값이 출력 XML로 추출한다에서 수행되어야하는 것으로 가정한다.

구분 기호를 사용하여 필요한 노드의 값을 연결하여 키를 정의 할 수 있습니다.

<xsl:key name="kProduct" match="Product" 
     use="concat(ProductDetail/ProductTitle, '|', Contact/ContactInfo/ContactNum, '|', Contact/ContactInfo/ContactSINum)" /> 

이어서 <Product> 노드를 통한 루프는 복합 키에 의해 그룹화 된 값의 첫 번째 인스턴스를 얻는다.

XSLT

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

    <xsl:key name="kProduct" match="Product" 
     use="concat(ProductDetail/ProductTitle, '|', Contact/ContactInfo/ContactNum, '|', Contact/ContactInfo/ContactSINum)" /> 

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

    <xsl:template match="Products"> 
     <Productcoll> 
      <xsl:for-each select="Product[generate-id() = generate-id(key('kProduct', concat(ProductDetail/ProductTitle, '|', Contact/ContactInfo/ContactNum, '|', Contact/ContactInfo/ContactSINum))[1])]"> 
       <ProductINFO> 
        <xsl:apply-templates select="key('kProduct', concat(ProductDetail/ProductTitle, '|', Contact/ContactInfo/ContactNum, '|', Contact/ContactInfo/ContactSINum))[1]/ProductDetail/ProductTitle" /> 
        <xsl:apply-templates select="key('kProduct', concat(ProductDetail/ProductTitle, '|', Contact/ContactInfo/ContactNum, '|', Contact/ContactInfo/ContactSINum))[1]/Contact/ContactInfo/ContactNum" /> 
        <xsl:apply-templates select="key('kProduct', concat(ProductDetail/ProductTitle, '|', Contact/ContactInfo/ContactNum, '|', Contact/ContactInfo/ContactSINum))[1]/Contact/ContactInfo/ContactSINum" /> 
       </ProductINFO>  
      </xsl:for-each> 
     </Productcoll> 
    </xsl:template> 
</xsl:stylesheet> 

출력

<Productcoll> 
    <ProductINFO> 
     <ProductTitle>title1</ProductTitle> 
     <ContactNum>1</ContactNum> 
     <ContactSINum>2</ContactSINum> 
    </ProductINFO> 
    <ProductINFO> 
     <ProductTitle>title2</ProductTitle> 
     <ContactNum>3</ContactNum> 
     <ContactSINum>4</ContactSINum> 
    </ProductINFO> 
</Productcoll> 
+0

고마워 ... 내 문제를 해결했습니다. – bigB

+0

답변이 도움이 되었다면 답을 받아 들일 수 있습니까? [답변 수락 방법] (https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) –

+0

수락 된 답변 .... 많이 고마워요. – bigB