2016-07-22 1 views
0

여기에서 달성하고자하는 것은 요소 (자식 노드 포함)를 이동 한 다음 해당 요소 내에 자식 노드를 추가하거나 그 반대의 경우입니다. 한 번에 한 가지만 할 수있는 것 같습니다. 동시에 둘 다 할 수 있습니까? 당신이 요소 CD3 ​​이동뿐만 아니라 추가의 자식 노드에서 볼 수 있듯이XSLT를 사용하여 XML 요소를 이동하고 하위 요소를 동시에 추가하는 방법은 무엇입니까?

다음은이

<catalog> 
<box1> 
    <cd1> 
     <title>Title 1</title> 
     <artist>Bob Dylan</artist> 
     <year>1985</year> 
    </cd1> 
    <cd2> 
     <title>Title 2</title> 
     <artist>Bonnie Tyler</artist> 
     <year>1988</year> 
    </cd2> 
    <cd3> 
     <title>Title 3</title> 
     <artist>Metallica</artist> 
     <year>1990</year> 
    </cd3> 
</box1> 

같은 출력을 하시겠습니까 내 입력 XML

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <box1> 
     <cd1> 
      <title>Title 1</title> 
      <artist>Bob Dylan</artist> 
      <year>1985</year> 
     </cd1> 
     <cd2> 
      <title>Title 2</title> 
      <artist>Bonnie Tyler</artist> 
      <year>1988</year> 
     </cd2> 
    </box1> 
    <box2> 
     <cd3> 
      <title>Title 3</title> 
      <artist>Metallica</artist> 
     </cd3> 
    </box2> 
</catalog> 

입니다.

다음은 코드의 순서에 관계없이 내가 한 것과 모든 요소를 ​​이동하는 것입니다.

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

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

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

    <!-- add a child element --> 
    <xsl:template match="cd3"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
      <year>1990</year> 
     </xsl:copy> 
    </xsl:template> 

    <!-- move node --> 
    <xsl:template match="/catalog"> 
     <xsl:copy> 
       <xsl:apply-templates /> 
       <xsl:copy-of select="box2/cd3"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="box2"/> 

</xsl:stylesheet> 

답변

0

변경 <xsl:apply-templates select="box2/cd3"/><xsl:copy-of select="box2/cd3"/><xsl:template match="/catalog/box1">-<xsl:template match="/catalog">을 변경합니다.

+0

Martin, 내가 제안한대로했는데 요소 이 표시되지 않습니다. – user1998820

+0

@ user1998820 네, 원래 스타일 시트에서 약간의 수정만을 제안하기는 어려웠습니다.'apply-templates'를 사용하라는 제안은 정확합니다. 또한''또한 접근법을 간소화하지만 간과 한 후에 apply-templates의 경로를''에 적용 할 필요가 있다는 것을 간과했다. –

0

아래 코드로 해결했습니다. 도움을 주셔서 감사합니다. 추가 조사를 시작했습니다.

<xsl:template match="/catalog"> 
    <xsl:copy> 
     <box1> 
      <xsl:apply-templates /> 
      <xsl:apply-templates select="box2/cd3"/> 
     </box1> 
    </xsl:copy> 
</xsl:template> 
관련 문제