2015-01-03 2 views
-1

XSLT를 사용하여 일부 HTML을 재구성하려고합니다. 나는이를 변화시킬 수 방법 :부모에게 속성 이동

<div> 
    <h2 class="foo">...</h2> 
    <p>bar</p> 
</div> 

로 :

<div class="foo"> 
    <h2>...</h2> 
    <p>bar</p> 
</div> 
+0

귀하의 의도는 분명하지만 이전 의견 제출자는 옳습니다. 현재 XSLT 코드를 표시하는 것을 잊었습니다. o r 작동하지 않는 것에 대해 알려주십시오. –

+0

@ PhilVallone : 몇 가지 다른 시도를했지만 어둠 속에서 우연히 발견됩니다. 나는 아래의 해답을 조사 할 것이다. - 고마워. –

답변

1

경우 당신 XSLT 1.0, 사용자의 입력에

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" encoding="UTF-8" indent="yes" /> 
<xsl:strip-space elements="*"/> 
    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="div"> 
    <xsl:copy> 
     <xsl:attribute name="class"> 
     <xsl:value-of select="h2/@class"/> 
     </xsl:attribute> 
     <xsl:apply-templates/> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="h2/@class"/> 
</xsl:stylesheet> 

적용 다음 XSLT 사용하고있는 출력이

<div class="foo"> 
<h2>...</h2> 
<p>bar</p> 
</div> 

첫 번째 템플릿 <xsl:template match="@*|node()">identity transformation입니다. 모든 속성과 다른 노드를 일치시키고이를 복사하여 모든 자식 노드와 현재 컨텍스트 노드의 속성에 identiy 변환을 적용합니다. 템플릿은 XSLT 2.0을 사용하는 경우

<xsl:attribute name="class"> 
     <xsl:value-of select="h2/@class"/> 
</xsl:attribute> 

사용하여 h2class 속성을 div 복사 div 일치하고 추가하면서

빈 템플릿 <xsl:template match="h2/@class"/>은이를 h2에서 class 속성을 제거합니다 조정될 수 있습니다

<xsl:attribute name="class" select="h2/@class"/> 
관련 문제