XSLT

2013-06-08 2 views
1

에서 모든 노드의 표현의 반복 노드 외부에 한 번 나타나는 내용을 표시하는 방법 나는 다음 몇 가지 헤더 정보 항목의 시리즈를 가지고 XML 데이터를 가지고있다. XSLT를 사용하여이를 헤더 영역과 일련의 항목과 함께 다른 형식으로 변환합니다. XSLT

그러나 포스트 번역 결과에서, 나는 단순히 같은 값을 반복한다하더라도, 단지 헤더에있는 데이터의 한 조각이 항목의 모든 인스턴스에 포함되어야합니다.

playlist_header_title=Playlist One 

    playlist_title=Playlist One 
    video_title=Video One 

    playlist_title=Playlist One 
    video_title=Video Two 

    playlist_title=Playlist One 
    video_title=Video Three 

내 XSLT는 매우 복잡하다 (:

샘플 데이터 (크게 단순화)

<rss> 
    <channel> 
    <title>Playlist One</title> 
    <items> 
     <item> 
     <title>Video One</title> 
     </item> 
     <item> 
     <title>Video Two</title> 
     </item> 
     <item> 
     <title>Video Three</title> 
     </item> 
    </items> 
    </channel> 
</rss> 

내 원하는 결과이 같은 것입니다 (이 값은 내가 할 수없는 하드 코드를 이렇게 변경 될 수 있습니다) 불행하게도 나는 내가 모든 것을, 나는 온라인으로 자신을 독학 한 무엇을하는지 모르겠지만) 내 머리에 약간의

대략 키 파이 나는 다른 사람으로부터 상속 eces는 다음과 같이 :

<xsl:template name="rss" match="/"> 
     <xsl:variable name="playlist_title"> 
     <xsl:value-of select="string(/rss/channel/title)"/> 
     </xsl:variable> 
     <xsl:for-each select="rss"> 
     <xsl:apply-templates name="item" select="channel/items/item"/> 
     </xsl:for-each> 
    </xsl:template> 

그럼 내가 여기에 포함되지 않습니다 "항목"라는 거대한 템플릿도하지만 기본적으로는 원하는대로 모든 항목의 데이터를 출력, 난 그냥 액세스하는 방법을 알아낼 수 없습니다 "playlist_title".

나는

<xsl:value-of select="string($playlist_title)"/> 

가 빈 반환 (템플릿 "항목"내부에서)를 호출 할 때. 변수가 for-each 루프 외부에서 생성되어 사용할 수 없기 때문에 이것이라고 가정합니다. (헤더의 결과 버전에서 for-each 루프 전에 출력 할 때 데이터를 올바르게 표시하지만 충분히 멀리 표시되지 않습니다.)

적용 할 때 with-param -templates를 사용하고 또한 with-param을 사용하여 다른 루프 내에서 call-template으로 변경하려고 시도했지만 공백도 표시합니다.

는 또한 문자열에 보내기보다는 내가 템플릿에 값을 통과 할 수 있어요 확인하기 위해 XML에서 PLAYLIST_TITLE를 당겨 시도했지만, 그들도 빈 나올. 예를 들어

:

<xsl:for-each select="channel/items/item"> 
    <xsl:call-template name="item"> 
     <xsl:with-param name="playlist_title">blah</xsl:with-param> 
    </xsl:call-template> 
</xsl:for-each> 


<xsl:template name="item"> 
    <xsl:param name="playlist_title" /> 
      playlist_title=<xsl:value-of select="string($playlist_title)"/> 
      video_title=... 
    ... 
    </xsl:template> 

이 값이 "어쩌구"그러나 다만 빈을 반환하지 않았다.

나는 난처한 상황에 빠진거야 (내 희망은 다음 XML에서 뽑아 PLAYLIST_TITLE 값 "ㅋ"를 대체했다, 그러나 아무도 돌파하지 않습니다)! 어떤 도움을 주셔서 감사합니다.

+1

시도'< xsl : value-of select = "../../ title"/>' – Pawel

답변

0

첫 번째 예에서는 item에 대한 템플릿 내부의 $playlist_title을 참조하려고 시도하는 중 변수가 해당 템플릿 안에 없기 때문에 실패합니다. 템플릿 일치 내에서 rss에 대해 생성되었으며 로컬 범위입니다.

다른 템플릿에 $playlist_title를 사용할 수 있도록하려면

, 당신은 "전역 범위"의 rss 템플릿 외부에서 선언해야합니다.예를 들어

: 두 번째 시도에 대해서는

<xsl:variable name="playlist_title"> 
    <xsl:value-of select="string(/rss/channel/title)"/> 
</xsl:variable> 
<xsl:template name="rss" match="/"> 
    <xsl:for-each select="rss"> 
     <xsl:apply-templates name="item" select="channel/items/item"/> 
    </xsl:for-each> 
</xsl:template> 

xsl:paramplaylist_title을 참조, 그것은 나를 위해 일했습니다. 참조 된 매개 변수의 이름과 선언 된 매개 변수의 이름 사이에 유형 -o 또는 다른 차이가 없는지 다시 한 번 확인하십시오.


당신은 xsl:variable 사용하여 원하는 출력을 달성하기 위해 이런 식으로 뭔가를 시도 할 수 있습니다 :

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" indent="yes"/> 

    <xsl:variable name="playlist_title" select="/rss/channel/title" /> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="rss/channel"/>   
    </xsl:template> 

    <xsl:template match="channel"> 
     <xsl:text>playlist_header_title=</xsl:text> 
     <xsl:value-of select="title"/> 
     <xsl:text>&#xA;&#xA;</xsl:text> 
     <xsl:apply-templates select="items/item"/> 
    </xsl:template> 

    <xsl:template match="item"> 
     <xsl:text>playlist_title=</xsl:text> 
     <xsl:value-of select="$playlist_title"/> 
     <xsl:text>&#xA;</xsl:text> 
     <xsl:text>video_title=</xsl:text> 
     <xsl:value-of select="title"/> 
     <xsl:text>&#xA;&#xA;</xsl:text> 
    </xsl:template> 

</xsl:stylesheet> 

을하거나 xsl:variable을 제거하고 xsl:param 사용할 수 있습니다

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" indent="yes"/> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="rss/channel"/>   
    </xsl:template> 

    <xsl:template match="channel"> 
     <xsl:text>playlist_header_title=</xsl:text> 
     <xsl:value-of select="title"/> 
     <xsl:text>&#xA;&#xA;</xsl:text> 
     <xsl:apply-templates select="items/item"> 
      <xsl:with-param name="playlist_title" select="title"/> 
     </xsl:apply-templates> 
    </xsl:template> 

    <xsl:template match="item"> 
     <xsl:param name="playlist_title" /> 
     <xsl:text>playlist_title=</xsl:text> 
     <xsl:value-of select="$playlist_title"/> 
     <xsl:text>&#xA;</xsl:text> 
     <xsl:text>video_title=</xsl:text> 
     <xsl:value-of select="title"/> 
     <xsl:text>&#xA;&#xA;</xsl:text> 
    </xsl:template> 

</xsl:stylesheet>