2016-03-21 9 views
0

私はこのようなXMLを持っていて、次のようにしたいと考えています。XSLソートノード(日付別)

-sort on employment_information/start_date、昇順。

- CompoundEmployeeノードを、最初にソートされたemployment_informationノードのみで返します。

注:日付形式は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> 

私はソートが考え出したが、どのように私は、最初のノードを選択しますか?

<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

日付でハイフンが同じ場所に常にあります。 'start-date'全体をソートしてハイフンを無視することができます。ハイフンはソート順に影響を与えません。 –

+0

スタイルシートがXSLT 1.0の場合、このタグ付きXSLT 2.0はなぜですか? –

+0

ソートが容易なので、XSLT 2.0ソリューションも歓迎します。私はちょうどタグを追加したが、それを削除したと思った。 – user2215655

答えて

2

このXSLT 1.0ソリューション試してください:あなたの入力例に

<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> 

応用し、その結果は以下のようになります。

<?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

ありがとう、これはトリックです! – user2215655

関連する問題