2016-05-13 4 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> 

Мне нужно переименовать большинство имен элементов, а затем отсортировать <order> дочерних элементов от нового имени элемента для использования в втором приложении. Мне также нужно добавить два пустых элемента. Я не контролирую порядок элементов в исходном 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> 
     <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> 

Но второе приложение нуждается в информации в следующем порядке:

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

Фактически, мне просто нужно переименовать «catalogNo» в качестве «V1» и двигаться так, чтобы это был первый элемент в каждом узле «заказ», а затем добавить пустые элементы «V2» и «V3». Все остальные элементы находятся в правильном порядке:

Есть ли механизм в XSLT для сортировки по имени нового элемента или каким-либо другим способом для получения моей информации в порядке, необходимом для моего второго приложения?

Спасибо.

ответ

0

Вариант 1

Вы можете заставить его вручную соответствие детей order, например, так:

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

Если вам действительно нужна сортировка вместо таких ручное решение, второй проход тоже может быть интересным. Я имею в виду: оставим теги V* несортированными, а затем запустите второй лист 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> 
Смежные вопросы