2013-09-30 2 views
2

XSLT를 처음 사용했습니다. XSLT를 사용하여 기존 자식 노드의 부모 노드를 추가하고 싶습니다.XSLT를 사용하여 부모 노드를 추가하는 방법

변환하기 전에

<Library> 
     . 
     .//There is more nodes here 
     . 
     <CD> 
      <Title> adgasdg ag</Title> 
      . 
      .//There is more nodes here 
      . 
     </CD> 
     . 
     .//There is more nodes here 
     . 
     <CLASS1> 
     <CD> 
      <Title> adgasdg ag</Title> 
      . 
      .//There is more nodes here 
      . 
     </CD> 
     </CLASS1> 
     </Library> 

변환 후

<Library> 
    <Catalog> 
    <CD> 
     <Title> adgasdg ag</Title> 
    </CD> 
    </Catalog> 
    <Class1> 
    <Catalog> 
     <CD> 
     <Title> adgasdg ag</Title> 
     </CD> 
    </Catalog> 
    </Class1> 
</Library> 

답변

3

이 요소 Catalog를 추가하려면 아래에 사용할 수있는 것처럼 나의 XML 파일은 같습니다

<?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="CD"> 
     <Catalog> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()" /> 
      </xsl:copy> 
     </Catalog> 
    </xsl:template> 
</xsl:stylesheet> 
2

을 내가 일반적으로 할 것이 정확히 무엇을 @markdark는 (항등 변환을 재정의) 제안하지만, 그렇지 않은 경우 Catalog을 추가하는 대신 다른 것을 수정해야합니다, 당신은 또한이 작업을 수행 할 수 ...

는 XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

    <xsl:template match="/*"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <Catalog> 
       <xsl:copy-of select="node()"/> 
      </Catalog> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
(도 1.0로 작동)
관련 문제