【问题标题】:Print child nodes打印子节点
【发布时间】:2020-10-07 11:26:48
【问题描述】:

我有以下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”时,我如何获取子节点(lrs)。 我希望有一些方法可以在其 TripOrder 时遍历到 Lr,以便我可以打印 LR

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 我假设您有一个运输订单类型=“TripOrder”的匹配模板?请张贴一些xsl
  • 我正在使用单个模板@ChristianMosz。
  • 我尝试了一些来自互联网的东西并没有完全锻炼@Rupesh_Kr。请帮忙
  • 在你预期的输出中,为什么显示2663,它的父transport-order类型不是TripOrder。

标签: xml xslt xslt-1.0


【解决方案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>&#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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 2014-05-23
      相关资源
      最近更新 更多