XSLT 속성 변환의 이중고

XSLT 속성 변환의 이중고

HTML로 변환할 XML 문서가 있습니다. 생략하고 싶은 불필요한 속성이 많지만, 캡쳐하고 싶은 속성은 아래와 같이 두 가지가 있습니다.

원천

<element att1="yes" att2="no" att3="yes">Text</element>

원하는 출력

<span class="att1">Text</span>

따라서 att1또는가 att2"예"이면 해당 속성 이름을 사용하여 클래스 속성이 생성되고 다른 모든 속성은 생략되며 요소는 범위로 변환됩니다.

소스 문서에서 att1및는 att2상호 배타적이어야 합니다. 하나가 "예"라고 대답하면 다른 하나는 존재할 수도 있고 존재하지 않을 수도 있습니다.

답변1

다음은 한 가지 접근 방식입니다(XSLT 2.0+).

<xsl:template match="element[(@att1, @att2)='yes']">
  <span class="{name((@att1, @att2)[.='yes'][1])}">
    <xsl:value-of select="."/>
  </span>
</xsl:template>

<xsl:template match="element">
  <!-- you haven't said what output you want if neither attribute is "yes" -->
</xsl:template>

관련 정보