2012-11-13 2 views
3

XSLT를 사용하여 XML을 XML로 변환하려고합니다. 출력 XML은 입력 XML의 ModificationTime 요소를 기준으로 정렬되어야합니다. 아래는 XML 코드입니다.XSLT를 사용하여 XML을 XML로 변환 후

<?xml version="1.0" encoding="UTF-8"?> 
<Process> 
<currentDayAndHour>@Fri16</currentDayAndHour> 

<!-- Few elements here. Need to retain them --> 


<rowCount>1</rowCount> 
<currentRow>1</currentRow> 


<ClientList> 
<Status>0</Status> 
<ServerResponse> 
    <Code>0</Code> 
    <Text>OK</Text> 
</ServerResponse> 
<ServiceStartTime>2012-11-09 16:06:42.786</ServiceStartTime> 
<ServiceEndTime>2012-11-09 16:06:42.827</ServiceEndTime> 
<Files> 
    <File> 
    <Name>test.20121107215230411.txt</Name> 
    <Size>29</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352343152</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
    <File> 
    <Name>test.20121107183757513.txt</Name> 
    <Size>29</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352331478</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
    <File> 
    <Name>test1.20121107215230500.txt</Name> 
    <Size>32</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352343152</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
    <File> 
    <Name>test1.txt</Name> 
    <Size>32</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352323788</ModificationTime> 
    <Owner>65174</Owner> 
    <Group>75431</Group> 
    </File> 
    <File> 
    <Name>HMP_test.txt</Name> 
    <Size>28</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352199478</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
    <File> 
    <Name>test1.20121107183757585.txt</Name> 
    <Size>32</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352331478</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
    <File> 
    <Name>client_access.20121108101411179.txt</Name> 
    <Size>4182</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352387653</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
    <File> 
    <Name>TechMtngAgenda.txt</Name> 
    <Size>107</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1352044842</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
    <File> 
    <Name>test.txt</Name> 
    <Size>29</Size> 
    <Type>Regular</Type> 
    <Permissions>-rw-r--r--</Permissions> 
    <ModificationTime>1350063313</ModificationTime> 
    <Owner>19737</Owner> 
    <Group>70902</Group> 
    </File> 
</Files> 
</ClientList> 
<currentDocument>1</currentDocument> 
</Process> 

모든 입력 요소가 포함 된 출력 XML이 필요하지만 파일 태그는 ModificationTime의 증가 순서로 각 파일을 포함해야합니다. 나는 XSLT를 처음 접했습니다. xsl : sort를 사용하여 시도했지만 원하는 결과를 얻을 수 없습니다.

답변

1

이 스타일 시트는 XSLT에서 당신이 원하는 것을 1.0

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

    <xsl:template match="Files"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates select="File"> 
     <xsl:sort order="ascending" data-type="number" select="ModificationTime"/> 
     </xsl:apply-templates>  
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

이 스타일 시트는 XSLT 2.0 당신이 원하는 무엇의 정체성 서식 문서를 복사하는 데 사용됩니다 두 경우 모두

<xsl:stylesheet 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs"> 

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

    <xsl:template match="Files"> 
    <xsl:copy> 
     <xsl:sequence select="@*"/> 
     <xsl:perform-sort select="File"> 
     <xsl:sort order="ascending" select="xs:integer(ModificationTime)"/> 
     </xsl:perform-sort>  
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

을 할 것 할 것 정확히 xsl:templateFiles 요소의 처리를 무시하기 위해 도입되었습니다. XSLT 1.0 예에서 xsl:sort과 함께 을 사용하면 File 요소를 ModificationTime으로 정렬 할 수 있습니다. <xsl:apply-templates select="File"> 정렬 된 File 요소의 처리는 Identity 템플리트로 다시 전달되므로 잠재적으로 추가로 재정의 될 수 있습니다.

XSLT 2.0 예제에서 xsl:sequence은 입력 노드를 결과 트리에 직접 삽입하는 데 사용됩니다. 마찬가지로 xsl:perform-sort은 요소를 복사하기위한 추가 지침을 수행하는 대신 정렬 된 시퀀스를 직접 반환합니다. 이러한 변경 사항은 스타일 시트의 실행을 더 빠르게 만들지 만 향후 유지 관리의 유연성을 감소시킬 수도 있습니다. 직접 항목을 선택할 때 재정의를 추가하는 것이 훨씬 어렵습니다. XSLT 1.0 스타일 시트 또는 그 스타일을 크게 변경하지 않고 XSLT 2.0에서 처리 할 수 ​​있습니다. 마지막으로,이 두 예제는 요소가 아닌 Files의 모든 자식을 node() 생략합니다. 즉 -

<xsl:apply-templates select="node()[not(self::File)]"/> 

또는 XSLT 2.0에서 1.0

<xsl:apply-templates select="node() except File"/> 

또는

<xsl:sequence select="node() except File"/> 
+0

은'except' 연산자는 XPath는 2.0에서만 사용할 수는 XSLT에 추가 할 수 있습니다 사람들을 캡처하려면 , XSLT 2.0에서. 수정 해주세요. –

+0

잡기에 감사드립니다. – nine9ths

+0

또한, (not (name() = 'File') 대신'not (self :: File)'을 사용하는 것이 더 편하고 (더 읽기 쉽고 더 효율적일 것입니다.) –

관련 문제