2014-03-19 2 views
2

내 XML 내가이형제 노드를 이전하는 것은

<xsl:template match="vanilla"> 
    <xsl:if test="IF THE ELEMENT RIGHT BEFORE THIS ONE IS CHOCOLATE OR BLACKFOREST"> 
     <p>After a great cake <xsl:value-of select="chocolate | blackforest"/></p> 
    </xsl:if> 

    <p>There is a vanilla cake <xsl:value-of select="."/></p> 
</xsl:template> 

처럼 작동 XSLT를 찾고 있어요이

<cakes> 
    <chocolate>for Tom</chocolate> 
    <vanilla>for Jim</vanilla> 
    <strawberry>for Harry</strawberry> 
    <vanilla>for Sue</vanilla> 
</cake> 

처럼 보이는이 케이크 중 하나 인 경우 출력은해야

After a great cake for Tom 
There is a vanilla cake for Jim 
There is a vanilla cake for Sue 

나는 그 답이 preceding-sibling :: * [1]과 관련이 있다고 의심하지만 이것이 특정 노드 종류인지 테스트하는 방법을 찾을 수 없습니다.

나는 asp.net에서 개발 중입니다.

답변

2

나는 그 답이 preceding-sibling :: * [1]과 관련이 있다고 생각하지만이 노드가 특정 노드인지 테스트 할 방법을 찾을 수 없습니다.

네, 그게 실제로 해결책입니다. 요소 이름이 name() 인지 확인하여 노드가 특정 요소인지 테스트 할 수 있습니다.

이 솔루션은 vanilla 요소 바로 앞에 오는 경우에만 chocolate 또는 blackforest 요소를보고합니다. 또한 제어 된 방식으로 텍스트를 출력하며 xsl:text 요소 내부에서만 텍스트를 출력합니다. 그래서 줄 바꿈을 XSLT 코드에 명시 적으로 추가해야합니다.

스타일 시트

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="text"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="vanilla"> 
     <xsl:if test="preceding-sibling::*[1][name() = 'chocolate' or name() = 'blackforest']"> 
      <xsl:text>After a great cake </xsl:text> 
      <xsl:value-of select="preceding-sibling::*[1]"/> 
      <xsl:text>&#10;</xsl:text> 
     </xsl:if> 
     <xsl:text>There is a vanilla cake </xsl:text> 
     <xsl:value-of select="."/> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:template> 

    <xsl:template match="text()"/> 

</xsl:stylesheet> 

출력 실제로

After a great cake for Tom 
There is a vanilla cake for Jim 
There is a vanilla cake for Sue 

1name() 요소의 완전한 자격을 갖춘 이름을 반환합니다. 요소 앞에 접두사가 있으면 접두어도 반환됩니다. local-name()을 사용하여 규정 된 이름의 "로컬"부분 만 출력 할 수 있습니다.

관련 문제