2016-05-13 5 views
1

私は、次のXMLがありますXSLT順序

<order-information> 
    <orderRecord> 
     <order> 
      <num_items>1</num_items> 
      <orderNo>CA79268</orderNo> 
      <ordDate>20160509</ordDate> 
      <colorCode>YEL</colorCode> 
      <sizeNo>LG</sizeNo> 
      <ordRemarks /> 
      <catalogNo>00407</catalogNo> 
     </order> 
     <order> 
      <num_items>1</num_items> 
      <orderNo>CA79268</orderNo> 
      <ordDate>20160509</ordDate> 
      <colorCode>BLU</colorCode> 
      <sizeNo>SM</sizeNo> 
      <ordRemarks /> 
      <catalogNo>00424</catalogNo> 
     </order> 
     <order> 
      <num_items>1</num_items> 
      <orderNo>CA79268</orderNo> 
      <ordDate>20160509</ordDate> 
      <colorCode>GRN</colorCode> 
      <sizeNo /> 
      <ordRemarks /> 
      <catalogNo>00499</catalogNo> 
     </order> 
    </orderRecord> 
</order-information> 

を私は、要素名のほとんどの名前を変更した後、第2のアプリケーションで使用するために新しい要素名で<order>子要素をソートする必要があります。私はまた、2つの空の要素を追加する必要があります。私はソースxmlの要素の順序を制御できません。私のXSLTの試み(方法および例については、このサイトを熟読時間の結果):

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

    <xsl:template match="text()"> 
     <xsl:value-of select="normalize-space(.)"/> 
    </xsl:template> 

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

    <!-- Order Record --> 
    <xsl:template match ="orderRecord/order"> 
     <xsl:element name="Group"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="order/catalogNo"> 
     <xsl:element name="V1"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
     <!-- Add empty "V2" and "V3" --> 
     <V2/> 
     <V3/> 
    </xsl:template> 
    <xsl:template match="order/num_items"> 
     <xsl:element name="V4"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="order/orderNo"> 
     <xsl:element name="V5"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="order/ordDate"> 
     <xsl:element name="V6"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="order/colorCode"> 
     <xsl:element name="V7"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="order/sizeNo"> 
     <xsl:element name="V8"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="order/ordRemarks"> 
     <xsl:element name="V9"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 

</xsl:transform> 

での結果:

<?xml version="1.0" encoding="UTF-8"?> 
<order-information> 
    <orderRecord> 
    <Group> 
     <V1>00407</V1> 
     <V2/> 
     <V3/> 
     <V4>1</V4> 
     <V5>CA79268</V5> 
     <V6>20160509</V6> 
     <V7>YEL</V7> 
     <V8>LG</V8> 
     <V9/> 
    </Group> 
    <Group> 
     <V1>00424</V1> 
     <V2/> 
     <V3/> 
     <V4>1</V4> 
     <V5>CA79268</V5> 
     <V6>20160509</V6> 
     <V7>BLU</V7> 
     <V8>SM</V8> 
     <V9/> 
    </Group> 
    <Group> 
     <V1>00499</V1> 
     <V2/> 
     <V3/> 
     <V4>1</V4> 
     <V5>CA79268</V5> 
     <V6>20160509</V6> 
     <V7>GRN</V7> 
     <V8/> 
     <V9/> 
    </Group> 
    </orderRecord> 
</order-information> 

<?xml version="1.0" encoding="UTF-8"?> 
<order-information> 
    <orderRecord> 
    <Group> 
     <V4>1</V4> 
     <V5>CA79268</V5> 
     <V6>20160509</V6> 
     <V7>YEL</V7> 
     <V8>LG</V8> 
     <V9/> 
     <V1>00407</V1> 
     <V2/> 
     <V3/> 
    </Group> 
    <Group> 
     <V4>1</V4> 
     <V5>CA79268</V5> 
     <V6>20160509</V6> 
     <V7>BLU</V7> 
     <V8>SM</V8> 
     <V9/> 
     <V1>00424</V1> 
     <V2/> 
     <V3/> 
    </Group> 
    <Group> 
     <V4>1</V4> 
     <V5>CA79268</V5> 
     <V6>20160509</V6> 
     <V7>GRN</V7> 
     <V8/> 
     <V9/> 
     <V1>00499</V1> 
     <V2/> 
     <V3/> 
    </Group> 
    </orderRecord> 
</order-information> 

しかし、第二のアプリケーションには、次の順序で情報を必要とします

実際には、 "catalogNo"の名前を "V1"に変更し、各 "order"ノードの最初の要素に移動し、空の要素 "V2"と "V3"を追加するだけで済みます。

2番目のアプリケーションで必要とされる順序で情報を取得するために、要素の新しい名前やその他の方法でソートするメカニズムがXSLTにありますか。

ありがとうございました。あなたは本当に、このようなのではなく、並べ替えが必要な場合

<!-- Order Record --> 
<xsl:template match ="orderRecord/order"> 
    <xsl:element name="Group"> 
     <xsl:apply-templates select="catalogNo"/> <!-- becomes V1, V2, V3 --> 
     <xsl:apply-templates select="num_items"/> <!-- becomes V4 --> 
     <xsl:apply-templates select="orderNo"/> <!-- becomes V5 --> 
     <xsl:apply-templates select="ordDate"/> <!-- becomes V6 --> 
     <!-- and so on --> 
    </xsl:element> 
</xsl:template> 

オプション2

:あなたがそうのように、手動でorderの子供を照合することによって、それを強制することができ

答えて

0

オプション1

手作りのソリューション、2番目のパスも面白いかもしれません。つまり、V*タグをソートしないままにしておき、そのタグをソートする目的の2番目のXSLTシートを実行します。

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

    <xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8"/> 

    <xsl:template match="text()"> 
    <xsl:value-of select="normalize-space(.)"/> 
    </xsl:template> 

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

    <!-- Sort Group's children --> 
    <xsl:template match="Group"> 
    <xsl:apply-templates select="*"> 
     <xsl:sort select="name()"/> 
    </xsl:apply-templates> 
    </xsl:template> 

</xsl:transform>