XSLT 1.0 Tansfomation — перемещение данных одноуровневого элемента в определенные одноуровневые элементы

У меня возникли проблемы с преобразованием xslt, и я был бы очень признателен за помощь. Я провел довольно много времени, используя различные методы в XPath и XQuery. Кроме того, я ограничен xslt 1.0.

Преобразование включает в себя внесение изменений в позиции продукта в файле заказа xml. Исходный XML-файл содержит элементы, однако некоторые элементы строки являются ссылками на купоны на скидку (см. dsc-102 и dsc-133 ниже). Чего мне нужно добиться, так это удалить узлы «orderDetails» для ссылок на купоны на скидку и добавить содержащую информацию к соответствующим элементам продукта одного уровня (см. пример преобразованного XML ниже). Каждая ссылка на купон на скидку указывает соответствующие позиции продукта в конце названия продукта (например, ….[glv-001][glv-003]).

Исходный XML-файл. Ниже приведен исходный XML-файл, содержащий 1 заказ с 3 товарными позициями и 2 ссылками на купоны на скидку. Код скидки «dsc-102» соответствует 2 позициям продукта «glv-001» и «glv-003». Код скидки «dsc-133» соответствует 1 позиции продукта «sho-123».

<xmldata>
<Order>
    <orderID>1010</orderID>
    <custFirstName>Jim</custFirstName>
    <custLastName>Jones</custLastName>
    <orderDetails>
        <productCode>sho-123</productCode>
        <productName>Leather Windsor Shoes - size 10</productName>
    </orderDetails>
    <orderDetails>
        <productCode>glv-001</productCode>
        <productName>Leather gloves - size Small</productName>
    </orderDetails>
    <orderDetails>
        <productCode>glv-003</productCode>
        <productName>Leather gloves - size XLarge</productName>
    </orderDetails>
    <orderDetails>
        <productCode>dsc-102</productCode>
        <productName>10% Discount for Leather Gloves [glv-001][glv-003]</productName>
    </orderDetails>
    <orderDetails>
        <productCode>dsc-133</productCode>
        <productName>Free Shipping for Windsor Shoes [sho-123]</productName>
    </orderDetails>
</Order>

Transformed XML File - Below is the transformed XML that I want to achieve. The transfer has removed both discount coupon references and added a 'discountCoupon' node to their corresponding sibling product items.

<xmldata>
<Orders>
    <orderID>1010</orderID>
    <custFirstName>Jim</custFirstName>
    <custLastName>Jones</custLastName>
    <orderDetails>
        <productCode>sho-123</productCode>
        <productName>Leather Windsor Shoes - size 10</productName>
        <discountCoupon>Free Shipping for Windsor Shoes</discountCoupon>
    </orderDetails>
    <orderDetails>
        <productCode>glv-001</productCode>
        <productName>Leather gloves - size Small</productName>
        <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
    </orderDetails>
    <orderDetails>
        <productCode>glv-003</productCode>
        <productName>Leather gloves - size XLarge</productName>
        <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
    </orderDetails>
</Orders>

What I have Tried So far - To be totally honest I have had quite limited success with this problem. The closest I have got with it has been with the following. However, it was pretty far off my intended result and 'matches' is a XLST 2.0 function and I am restricted to version 1.

<xsl:if test="../OrderDetails[ProductCode = 'DSC-15'] and matches(ProductCode,'AH010585059',i)">DiscountCoupon</xsl:if>

Если бы кто-то мог помочь мне с этой проблемой или подтолкнуть меня в правильном направлении, я был бы очень признателен.

-Ваше здоровье.


person urbanMethod    schedule 05.01.2012    source источник


Ответы (2)


Решение, похожее на решение @lwburk, но более простое — без <xsl:if> и без <xsl:variable>:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match=
  "orderDetails[not(starts-with(productCode, 'dsc-'))]">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
   <xsl:apply-templates mode="coupon" select=
   "../orderDetails[starts-with(productCode, 'dsc-')]
                    [contains(productName,
                           concat('[', current()/productCode, ']')
                          )
                     ]/productName
   "/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="orderDetails[starts-with(productCode, 'dsc-')]"/>

 <xsl:template match="productName" mode="coupon">
  <discountCoupon>
   <xsl:value-of select="substring-before(., ' [')"/>
  </discountCoupon>
 </xsl:template>
</xsl:stylesheet>

когда это преобразование применяется к предоставленному XML-документу:

<Order>
    <orderID>1010</orderID>
    <custFirstName>Jim</custFirstName>
    <custLastName>Jones</custLastName>
    <orderDetails>
        <productCode>sho-123</productCode>
        <productName>Leather Windsor Shoes - size 10</productName>
    </orderDetails>
    <orderDetails>
        <productCode>glv-001</productCode>
        <productName>Leather gloves - size Small</productName>
    </orderDetails>
    <orderDetails>
        <productCode>glv-003</productCode>
        <productName>Leather gloves - size XLarge</productName>
    </orderDetails>
    <orderDetails>
        <productCode>dsc-102</productCode>
        <productName>10% Discount for Leather Gloves [glv-001][glv-003]</productName>
    </orderDetails>
    <orderDetails>
        <productCode>dsc-133</productCode>
        <productName>Free Shipping for Windsor Shoes [sho-123]</productName>
    </orderDetails>
</Order>

получен желаемый правильный результат:

<Order>
   <orderID>1010</orderID>
   <custFirstName>Jim</custFirstName>
   <custLastName>Jones</custLastName>
   <orderDetails>
      <productCode>sho-123</productCode>
      <productName>Leather Windsor Shoes - size 10</productName>
      <discountCoupon>Free Shipping for Windsor Shoes</discountCoupon>
   </orderDetails>
   <orderDetails>
      <productCode>glv-001</productCode>
      <productName>Leather gloves - size Small</productName>
      <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
   </orderDetails>
   <orderDetails>
      <productCode>glv-003</productCode>
      <productName>Leather gloves - size XLarge</productName>
      <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
   </orderDetails>
</Order>

Объяснение: Надлежащее использование и переопределение правила идентификации и templates/pattern-matching.

person Dimitre Novatchev    schedule 06.01.2012

Следующая таблица стилей дает правильный результат:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="orderDetails[not(starts-with(productCode, 'dsc'))]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:variable name="discount"
                select="../orderDetails[starts-with(productCode, 'dsc') and 
                      contains(productName, 
                         concat('[', current()/productCode, ']'))]/productName"/>
            <xsl:if test="$discount">
                <discountCoupon>
                    <xsl:value-of select="substring-before($discount, ' [')"/>
                </discountCoupon>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="orderDetails[starts-with(productCode, 'dsc')]"/>
</xsl:stylesheet>

Примечания и пояснения:

  • Преобразование идентичности копирует большинство узлов без изменений.
  • Скидки копируются в нескидочные элементы по мере их обработки.
  • Скидка orderDetails игнорируется
person Wayne    schedule 06.01.2012