2012-06-27 6 views
2

나는 다음과 같은 XML이 :XML 병합 노드

<Root>  
     <Item> 
      <CityCode>ALV</CityCode> 
      <AirportCode>ALV</AirportCode> 
      <CityName>ANDORRA LA VELLA</CityName> 
      <AirportName>Andorra La Vella Hlpt</AirportName> 
      <StateCode></StateCode> 
      <CountryCode>AD</CountryCode> 
      <AirportTypeCode>8</AirportTypeCode> 
      <AirportTypeName>Heliport, not scheduled</AirportTypeName>  
     </Item>  
</Root> 

을 그리고 난이 얻을 수있는 XSLT를 필요

내가 처음이 개 노드를 얻는 방법을 알고
<Root> 
    <Item> 
    <CityCode>ALV</CityCode> 
    <CityName>ANDORRA LA VELLA</CityName> 
    <CityNameComplete> ANDORRA LA VELLA (ALV) - Andorra La Vella Hlpt </CityNameComplete> 
    <Item> 
<Root> 

, 나도 몰라 마지막 "삽입"하는 법.

답변

0

다음은 누락 된 문자를 처리 할 수있는 버전입니다 (<AirportCode> 및/또는).:

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

    <!--Identity template. Matches everything that isn't matched 
     by another template and copies it without modification.--> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Item"> 
     <!--Copy the current element (Item) to the output.--> 
     <xsl:copy> 
      <!--Apply templates to attributes of Item and also the 
      CityCode and CityName elements. All other nodes of 
      Item will not be processed.--> 
      <xsl:apply-templates select="CityCode|CityName|@*"/> 
      <CityNameComplete> 
       <!--Output the value of CityName.--> 
       <xsl:value-of select="CityName"/> 
       <!--Apply the template for AirportCode. If it doesn't exist, 
       nothing will be output.--> 
       <xsl:apply-templates select="AirportCode"/> 
       <!--Apply the template for AirportName. If it doesn't exist, 
       nothing will be output.--> 
       <xsl:apply-templates select="AirportName"/> 
      </CityNameComplete> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="AirportCode"> 
     <!--Output the value of AirportCode, surrounded by parenthesis and 
     preceded by a space.--> 
     <xsl:value-of select="concat(' (',.,')')"/> 
    </xsl:template> 

    <xsl:template match="AirportName"> 
     <!--Output the value of AirportName, preceded by a "space dash space".--> 
     <xsl:value-of select="concat(' - ',.)"/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

어떻게 작동 하는가에 대한 자세한 설명은 어디입니까? 그것이 그렇듯이,이 대답은 미래의 방문객에게별로 도움이되지 않습니다. –

+0

@ O.R.Mapper - 코드가 충분히 간단하여 자세한 설명이 필요하다고 생각하지 않았습니다. OP는 답변을 수락하기 전에 질문이 없었으며 향후 방문객이 설명을 요청할 수도있었습니다. 코드가하는 일을 설명하기 위해 주석을 추가했습니다. 의견에 포함되지 않은 특정 질문이있는 경우 알려주십시오. –

+0

아, 더 자주는 아니지만, 인터넷의 한 가지 상수는 원저자가 미래 방문자가 실제로 묻는 시간에 더 이상 응답하지 않는다는 것입니다 .- 너무 늦기 전에 설명을 즉시 제공하는 것이 더 낫습니다. . –

1

이것을 전체 XSLT에 삽입하는 방법에는 여러 가지가 있지만 <CityNameComplete> 요소를 원본 Xml 문서의 <AirportName> 요소로 대체한다고 가정 해 보겠습니다.

<xsl:template match="/Root/Item/AirportName"> 
    <CityNameComplete><xsl:value-of select="../CityName"/> (<xsl:value-of select="../AirportCode"/>) - <xsl:value-of select="."/></CityNameComplete> 
</xsl:template> 

이이 결과에

<CityNameComplete>ANDORRA LA VELLA (ALV) - Andorra La Vella Hlpt</CityNameComplete> 

요소를 얻을 것입니다 :

그런 다음, 키는 XSLT의 <value-of> 요소입니다.

는 업데이트 :

<xsl:template match="/Root/Item/AirportName"> 
    <CityNameComplete><xsl:text> </xsl:text><xsl:value-of select="../CityName"/> (<xsl:value-of select="../AirportCode"/>) - <xsl:value-of select="."/><xsl:text> </xsl:text></CityNameComplete> 
</xsl:template> 

결과 :

<CityNameComplete> ANDORRA LA VELLA (ALV) - Andorra La Vella Hlpt </CityNameComplete> 

업데이트 2 : 당신이 정말로 전체 도시 이름 주위의 공백을 원하는 경우, 스타일 시트에 <xsl:text> </xsl:text>로 추가 포함 IATA 코드.

은 업데이트 3 :

<xsl:template match="/Root/Item"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
     <CityNameComplete><xsl:text> </xsl:text><xsl:value-of select="CityName"/> (<xsl:value-of select="AirportCode"/>) - <xsl:value-of select="AirportName"/><xsl:text> </xsl:text></CityNameComplete> 
    </xsl:copy> 
</xsl:template> 

주 당신이 당신이 원하지 않는 노드를 제거해야합니다 : 노드를 삽입하기위한 대안 솔루션은 <Item> 요소에 대한 템플릿을 추가하는 것입니다 정확히 당신의 결과물을 얻으십시오 - 당신이 요구하지 않았던 것처럼, 나는 답을 혼란스럽게하고 싶지 않았기 때문에 그렇게하는 법에 대한 어떤 코드도 포함하지 않았습니다. 이 작업을 수행하는

+0

감사합니다. 매퍼, 완벽하게 작동했습니다. –

+0

필자는 필요없는 노드를 제거 할 수 있었지만 루트 및 항목을 유지하는 방법을 잘 모르겠습니다. XSLT의 정체성 서식 파일에 대한 설명을 참조하십시오, 예를 들면 : - 안도라 라 베야 Hlpt

+0

@OanaAnton ALV 안도라 라 베야 안도라 라 베야 (ALV) : 내 출력이 있습니다 여기 : http://dev.ektron.com/kb_article.aspx?id=492 기본적으로 XSLT의 다른 템플릿과 일치하는 노드를 제외하고 소스 XML 문서의 전체 내용을 출력으로 복사합니다. –

1

방법 중 하나는 정체성 템플릿에 구축하고, 항목 요소에 맞게 별도의 템플릿을 가지고있는 당신이 다음 출력 CONCAT 기능을 사용하여 CityNameComplete 요소 :

<xsl:template match="Item"> 
    <Item> 
    <xsl:apply-templates select="@*|node()"/> 
    <CityNameComplete> 
     <xsl:value-of select="concat(CityName, ' (', AirportCode, ') ', AirportName)"/> 
    </CityNameComplete> 
    </Item> 
</xsl:template> 

출력하고 싶지 않은 모든 요소를 ​​무시하려면 템플릿이 필요합니다. 소자들 CityCodeCityName이 아닌 다른이 경우 모든 것을 여기에

<xsl:template match="Item/*[not(self::CityCode|self::CityName)]"/> 

이 입력 XML에 적용되는 전체 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="Item/*[not(self::CityCode|self::CityName)]"/> 

    <xsl:template match="Item"> 
     <Item> 
     <xsl:apply-templates select="@*|node()"/> 
     <CityNameComplete> 
      <xsl:value-of select="concat(CityName, ' (', AirportCode, ') ', AirportName)"/> 
     </CityNameComplete> 
     </Item> 
    </xsl:template> 

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

이며, 다음과 같은 출력

<Root> 
    <Item> 
    <CityCode>ALV</CityCode> 
    <CityName>ANDORRA LA VELLA</CityName> 
    <CityNameComplete>ANDORRA LA VELLA (ALV) Andorra La Vella Hlpt</CityNameComplete> 
    </Item> 
</Root> 
입니다
+0

+1입니다. –