打印子节点

Print child nodes

我有以下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

当 transport-order type="TripOrder" 时,我如何获取子节点(lrs)。 我希望有某种方法可以在它的 TripOrder 时遍历 Lr,以便我可以打印 LRs

试试这个:

    <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

查看转换

这应该会给您一些想法。希望您可以使用它。如果没有,请 post 您想要的 XML 输出。

  <xsl:template match="number[not(@nil = 'true')]">
    <xsl:text>&#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>