2016-03-21 6 views
0

ich eine XML wie diese haben, und wollen:XSL Art Knoten nach Datum, das Rück ersten Knoten

-sort auf employment_information/start_date, aufsteigend.

-Return den CompoundEmployee Knoten mit nur dem ersten sortierten employment_information Knoten.

Hinweis: Datumsformat yyyy-mm-dd

<?xml version="1.0" encoding="UTF-8"?> 
<queryCompoundEmployeeResponse> 
<CompoundEmployee> 
    <person> 
    <person_id>913</person_id> 
    <person_id_external>uat_dddd</person_id_external> 
    <employment_information> 
    <custom_string1>aaaa</custom_string1> 
    <start_date>2015-01-01</start_date> 
    <end_date>2015-12-31</end_date> 
    <user_id>uat_aaaa</user_id> 
    </employment_information> 
    <employment_information> 
    <custom_string1>bbbb</custom_string1> 
    <start_date>2016-01-01</start_date> 
    <end_date>2016-12-31</end_date> 
    <user_id>uat_bbbb</user_id> 
    </employment_information> 
    </person> 
</CompoundEmployee> 
<CompoundEmployee> 
<person> 
    <person_id>914</person_id> 
    <person_id_external>uat_dddd</person_id_external> 
    <employment_information> 
    <custom_string1>cccc</custom_string1> 
    <start_date>2016-02-01</start_date> 
    <end_date>2016-12-31</end_date> 
    <user_id>uat_cccc</user_id> 
    </employment_information> 
    <employment_information> 
    <custom_string1>dddd</custom_string1> 
    <start_date>2015-02-01</start_date> 
    <end_date>2015-12-31</end_date> 
    <user_id>uat_dddd</user_id> 
    </employment_information> 
    </person> 
</CompoundEmployee> 
</queryCompoundEmployeeResponse> 

Ich habe die Sortierung herausgefunden, aber wie wähle ich den ersten Knoten?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:template match="queryCompoundEmployeeResponse/CompoundEmployee/person"> 
<xsl:copy> 
    <xsl:apply-templates select="employment_information"> 
    <!-- concat year, month, day --> 
    <xsl:sort select="concat(
       substring(start_date, 1, 4), 
       substring(start_date, 6, 2), 
       substring(start_date, 9, 2) 
      )" order="ascending"/> 
    </xsl:apply-templates> 
</xsl:copy> 
</xsl:template> 
<xsl:template match="@* | node()"> 
<xsl:copy> 
    <xsl:apply-templates select="@* | node()"/> 
</xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 
+0

Die Bindestriche in die Tage sind immer an der gleichen Stelle. Sie können einfach das gesamte Startdatum sortieren und die Bindestriche ignorieren, da sie die Sortierreihenfolge nicht beeinflussen. –

+0

Warum ist dieses XSLT 2.0 markiert, während Ihr Stylesheet XSLT 1.0 ist? –

+0

Eine XSLT 2.0-Lösung ist ebenfalls willkommen, da das Sortieren einfacher ist. Dachte, ich habe gerade das Tag hinzugefügt, aber es entfernt. – user2215655

Antwort

2

dieses XSLT 1.0 Lösung Versuchen:

<xsl:stylesheet version="1.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="*"/> 

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

<xsl:template match="person"> 
    <xsl:copy> 
     <xsl:apply-templates select="person_id | person_id_external"/> 
     <xsl:for-each select="employment_information"> 
      <xsl:sort select="start_date" data-type="text" order="ascending"/> 
      <xsl:if test="position()=1"> 
       <xsl:copy-of select="."/> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

auf Ihre Eingabe Beispiel angewendet wird, wird das Ergebnis sein:

<?xml version="1.0" encoding="UTF-8"?> 
<queryCompoundEmployeeResponse> 
    <CompoundEmployee> 
     <person> 
     <person_id>913</person_id> 
     <person_id_external>uat_dddd</person_id_external> 
     <employment_information> 
      <custom_string1>aaaa</custom_string1> 
      <start_date>2015-01-01</start_date> 
      <end_date>2015-12-31</end_date> 
      <user_id>uat_aaaa</user_id> 
     </employment_information> 
     </person> 
    </CompoundEmployee> 
    <CompoundEmployee> 
     <person> 
     <person_id>914</person_id> 
     <person_id_external>uat_dddd</person_id_external> 
     <employment_information> 
      <custom_string1>dddd</custom_string1> 
      <start_date>2015-02-01</start_date> 
      <end_date>2015-12-31</end_date> 
      <user_id>uat_dddd</user_id> 
     </employment_information> 
     </person> 
    </CompoundEmployee> 
</queryCompoundEmployeeResponse> 
+0

Danke, das macht den Trick! – user2215655

Verwandte Themen