2014-09-08 4 views
1

를 사용하여 XML의 하나 개 이상의 자식 노드에 부모 노드를 추가하고 난 다음 형식에서 XML을 추적 케이스를내가 XSLT에 새로운 오전 XSLT

를 달성하기 위해 노력하고

<A> 
    <B>..</B> 
    <C>..</C> 
    .. 
    <Z>..</Z> 
</A> 

나는 내가 다음 XSLT 코드를 작성하므로 최종 XML은이를 위해

<A> 
    <aa> 
     <B>..</B> 
     <C>..</C> 
     .. 
     <X>...</X> 
    </aa> 
</A> 

로 변환 얻을 것이다 직후 새로운 노드를 추가하려고

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

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

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

이를 사용하여 나는 다음과 같은 출력

<A> 
    <aa> 
     <B>..</B> 
    </aa> 
     <C>..</C> 
     .. 
     <X>..</X> 
</A> 

내가 원하는 출력을 달성하기 위해 XSLT로 만들 필요가 변경 어떤 종류의 확실하지 않다에게 얻을

답변

0

하는 경우 A의 모든 자식을 단일aa으로 묶으려는 경우가 아닌 A과 일치하는 템플릿에서이 작업을 수행해야합니다..

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

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

    <xsl:template match="A"> 
    <xsl:copy><!-- copy the A --> 
     <xsl:apply-templates select="@*" /><!-- attributes, if any --> 
     <aa><!-- insert the extra aa --> 
     <xsl:apply-templates /><!-- process children --> 
     </aa> 
    </xsl:copy> 
    </xsl:template> 

    <!-- this may be a typo in the question, but for reference, here's how 
     to rename Z to X. If you don't need to do this, just leave this template 
     out and let the identity template at the top handle it. --> 
    <xsl:template match="Z"> 
    <X> 
     <xsl:apply-templates select="@*|node()"/> 
    </X> 
    </xsl:template> 
</xsl:stylesheet> 
+0

알겠습니다. 나는 내 접근 방식의 실수를 이해했다. – user1198477