Печать дочерних узлов

#xml #xslt #xslt-1.0

#xml #xslt #xslt-1.0

Вопрос:

У меня есть следующий xml

 <invoice>
  <transport-orders type="array">
    <transport-order type="Lr">
      <number>2663</number>
      <consignor-city>Mumbai</consignor-city>
      <consignee-city>Bangalore</consignee-city>
    </transport-order>
    <transport-order type="TripOrder">
      <number nil="true"/>
      <consignor-city>Bhiwandi</consignor-city>
      <consignee-city>Mumbai</consignee-city>
      <type>TripOrder</type>
      <charges>
        <toll-charge>100.0</toll-charge>
        <notes>
          <loading-charge>90010, 90011</loading-charge>
        </notes>
      </charges>
      <lrs type="array">
        <lr>
          <number>90010</number>
          <consignor-city>Bhiwandi</consignor-city>
          <consignee-city>Mumbai</consignee-city>
          <type>Lr</type>
        </lr>
        <lr>
          <number>90011</number>
          <consignor-city>Bhiwandi</consignor-city>
          <consignee-city>Mumbai</consignee-city>
          <type>Lr</type>
        </lr>
      </lrs>
    </transport-order>
    <transport-order type="Lr">
      <number>2664</number>
      <consignor-city>Mumbai</consignor-city>
      <consignee-city>Bangalore</consignee-city>
      <type>Lr</type>
    </transport-order>
  </transport-orders>
</invoice>
  

Я хочу напечатать все данные LR по строкам (номер, отправитель-город, получатель-город).

Ниже приведен мой код, и я попытался распечатать lrs

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

    <xsl:template match="invoice">
      <xsl:apply-templates mode="second-page-details" select="transport-orders/transport-order"/>
    </xsl:template>

    <xsl:template match="*" mode="second-page-details" >
        <xsl:choose>
          <xsl:when test="type = 'TripOrder'">
            <xsl:template match="/">
              <td style="width:4.5%;border-right:solid 1px;"> <xsl:value-of select="number"/></td>
            </xsl:template>
          </xsl:when>
          <xsl:otherwise>
              <td style="width:4.5%;border-right:solid 1px;"> <xsl:value-of select="number"/></td>
          </xsl:otherwise>
        </xsl:choose>
    </xsl:template> 
</xsl:stylesheet>
  

Я хочу вывести все данные Lr в виде

 2663
90010
90011
2664
  

Когда тип транспортного заказа =»TripOrder», как мне использовать дочерние узлы (lr).
Я надеялся, что есть какой-то способ перейти к Lr, когда он отключен, чтобы я мог печатать LRS

Комментарии:

1. что вы пробовали до сих пор?

2. Я предполагаю, что у вас есть шаблон соответствия для transport-order type=»TripOrder»? Пожалуйста, опубликуйте некоторые xsl

3. Я использую один шаблон @ChristianMosz.

4. Я попробовал несколько вещей из Интернета, которые точно не тренировались @Rupesh_Kr. Пожалуйста, помогите

5. В вашем ожидаемом выводе, почему вы показываете 2663, его родительский тип транспортного заказа не является TripOrder.

Ответ №1:

Попробуйте это:

     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    
    <xsl:template match="/">
        <table>
            <thead>
                <tr>
                    <th>Number</th>
                    <th>Consignor-City</th>
                    <th>Consignee-City</th>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="//transport-order">
                    <xsl:choose>
                        <xsl:when test="@type = 'Lr'">
                            <xsl:call-template name="TRCreation"/>
                        </xsl:when>
                        <xsl:when test="@type = 'TripOrder'">
                            <xsl:for-each select=".//lr">
                                <xsl:call-template name="TRCreation"/>
                            </xsl:for-each>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            </tbody>
        </table>
    </xsl:template>
    
    <xsl:template name="TRCreation">
        <tr>
            <td><xsl:value-of select="number"/></td>
            <td><xsl:value-of select="consignor-city"/></td>
            <td><xsl:value-of select="consignee-city"/></td>
        </tr>
    </xsl:template>
    
</xsl:stylesheet>
  

смотрите Преобразование в https://xsltfiddle .liberty-development.net/93nwgDC

Ответ №2:

Это должно дать вам несколько идей. Надеюсь, вы сможете с этим работать. Если нет, пожалуйста, отправьте желаемый вывод XML.

   <xsl:template match="number[not(@nil = 'true')]">
    <xsl:text>amp;#13;</xsl:text>
    <td style="width:4.5%;border-right:solid 1px;">
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

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