2014-12-24 2 views
1

XML로 역 분개 할 수 있어야하는 시험을 배우고 있습니다. 나는 책의 순서를 뒤집거나 그 책에서 요소의 순서를 뒤집어 쓰려고합니다.xslt를 사용하여 XML의 내용을 뒤집을 수 있습니다.

입력 XML은 다음과 같습니다 책의

<library> 
    <book> 
     <author>James Joyce</author> 
     <title>Ulysses</title> 
     <year>1922</year> 
    </book> 
    <book> 
     <author>Alois Jirasek</author> 
     <title>Psohlavci</title> 
     <year>1910</year> 
    </book> 
    <book> 
     <author>Tomas Hruska</author> 
     <title>Pascal pro zacatecniky</title> 
     <year>1989</year> 
    </book> 
    </library> 

반전 순서 : 항목의

<library> 
    <book> 
     <author>Tomas Hruska</author> 
     <title>Pascal pro zacatecniky</title> 
     <year>1989</year> 
    </book> 
    <book> 
     <author>Alois Jirasek</author> 
     <title>Psohlavci</title> 
     <year>1910</year> 
    </book> 
    <book> 
     <author>James Joyce</author> 
     <title>Ulysses</title> 
     <year>1922</year> 
    </book> 
    </library> 

반전하기 위해 책 :

<library> 
    <book> 
     <year>1922</year> 
     <title>Ulysses</title> 
     <author>James Joyce</author> 
    </book> 
    <book> 
     <year>1910</year> 
     <title>Psohlavci</title> 
     <author>Alois Jirasek</author> 
    </book> 
    <book> 
     <year>1989</year> 
     <title>Pascal pro zacatecniky</title> 
     <author>Tomas Hruska</author> 
    </book> 
    </library> 

나는 것을 들었다 아주 쉽기 때문에 너무 멋진 것을 시도하지 마십시오. apply-templates와 value-of를 통해 for-each 또는 재귀를 통해 기본 루프를 사용할 수 있습니다.

+1

그리고 당신의 질문은? 고려하십시오 : "다만 저에게 방법을 보여주십시오"인 경우에, 당신은 아무것도 배우지 않으며 아마 시험에 실패하지 않을 것이다. – usr2564301

+1

글쎄, 이미 두 번 복사하는 방법, 텍스트를 복사하는 방법을 발견했지만, 그것을 반대로 실패, 어디서부터 시작 해야할지 모르겠다 원인. 오늘 저는 방금 xslt가 존재한다는 것을 알았습니다. 그러니 제발 나를 비난하지 마시고, 제 질문은 어떻게하는 겁니다. – Dracke

+0

나는 모든 책과 일치하는 책에서 그 내용을 뒤집을 수 있으며, 제목과 내용보다는 내용이 모두 포함 된 첫 번째 글쓰기 년보다는 더 복잡하다고 생각합니다. 그리고 또한 도서관 내가 잘못하지 않으면 책이 쓰여지지 않을 것입니다. – Dracke

답변

1

이 시도

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<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> 

<xsl:template match="library"> 
    <library> 
    <xsl:for-each select="book"> 
    <xsl:sort select="position()" order="descending"/> 
     <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy> 
    </xsl:for-each> 
    </library> 
</xsl:template> 

</xsl:stylesheet> 

두 번째 유형 :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<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> 

<xsl:template match="book"> 
    <book> 
    <xsl:for-each select="*"> 
    <xsl:sort select="position()" order="descending"/> 
     <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy> 
    </xsl:for-each> 
    </book> 
</xsl:template> 

</xsl:stylesheet> 
+0

고마워요, 나는 처음에 모든 것을 복사 할 수 있고 그 위치를 뒤집을 수 있다는 것을 몰랐습니다. 이것은 내가 문제를 해결하는데 정말로 도움이되었습니다. – Dracke

+0

@Dracke, 오신 것을 환영합니다. 많은 샘플이 STACKOVERFLOW 사이트에 있으며, 매우 유용합니다. –

0

가장 간단한 방법은 위치()로 내림차순으로 노드를 sort입니다.

첫 번째 유형 분류 :

+0

첫 번째와 두 번째 정렬에 대해 어떻게해야합니까? – Dracke

+0

당신은 그와 같은 구체적인 것을 일치시킬 필요가 없습니다. [신원 변환 템플릿] (http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT) (xsl : sort가 추가됨)을 사용하여 책과 해당 속성 모두에 재귀 적으로 적용 할 수 있습니다. –

0

그것은 꽤 간단합니다

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

    <xsl:template match="/library"> 
     <library> 
      <xsl:for-each select="book"> 
       <xsl:sort select="author" order="descending"/> 
       <xsl:copy-of select="."/> 
      </xsl:for-each> 
     </library> 
    </xsl:template> 
</xsl:stylesheet> 
관련 문제