xslt - XSL 新手,需要一些建议

标签 xslt

然而,这个有点不同,因为我对 XSL 完全一无所知,并且需要让这个模板适用于我们的运输软件。

我已经让它做我想做的大部分事情,就像我对大多数其他语言所做的那样,边学习边学习。但我似乎无法实现我需要的递归循环。

基本上,我需要它使用 $order/Item/Quantity 的数值来确定 for-each 中每个“Name”变量的循环次数。

它当前将按预期打印行项目,但只打印其中之一。我玩过这里发布的各种不同的递归模板,但由于我缺乏 XSL 知识,我无法让它工作,所以已经恢复到原来的(在我开始处理数量部分之前) )。

这是整个模板,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sw="http://www.interapptive.com/shipworks" extension-element-prefixes="sw">

    <!-- Imports -->
    <xsl:import href="System\Snippets" />

    <xsl:output method="html" encoding="utf-8" indent="yes" />

    <!-- Start of template -->
    <xsl:template match="/"><xsl:apply-templates /></xsl:template>
    <xsl:template match="ShipWorks">

    <!-- Controls display of thumbnail images -->
    <xsl:variable name="thumbnailsEnabled" select="false()" />
    <xsl:variable name="thumbnailWidth" select="'50px'" />
    <xsl:variable name="thumbnailHeight" select="'50px'" />

    <!-- Width defined by the template PageSettings -->
    <xsl:variable name="pageWidth" select="concat(Template/Output/ContentWidth, ' in')" />

    <!-- Default font.  Specified as a variable since GMail and Outlook behave differently. -->
    <xsl:variable name="pageFont" select="'font-family: Arial; font-size: 8pt;'" />

    <!-- These styles are used on multiple td's so to avoid copy\paste errors they are defined once here.  We have to do this since GMail doesn't support <style> in the <head>.  -->
    <xsl:variable name="orderDetailHeaderStyle" select="'border: 1px solid dimgray; background-color: #F3F3F3; font-weight: bold; padding: 3px;'" />
    <xsl:variable name="orderDetailAttributeStyle" select="'color: #808080; padding: 0px 8px 2px 2px;'" />
    <xsl:variable name="orderChargeStyle" select="'white-space: nowrap; text-align: right; padding: 1px 8px 3px 16px;'" />

    <html>

    <head>
        <title>Can label - gallon - Paints/Primers</title>

        <style>
            body, table { <xsl:value-of select="$pageFont" /> }
        </style>

    </head>

    <body style="{$pageFont}">

        <xsl:variable name="orderCount" select="count(Customer/Order)" />
        <xsl:if test="$orderCount = 1">

        <xsl:variable name="order" select="Customer/Order[1]" />
             
            <xsl:for-each select="$order/Item">
                
                    <table style="width: 100%; height: 100%;" cellspacing="0">
                      <tr>
                       <td style="{$orderDetailAttributeStyle}; width: 100%; height: 100%; vertical-align: middle;" align="center">
                        <!-- this is what needs to be repeated based on the value of $order/Item/Quantity -->
                        <xsl:choose>
                            <xsl:when test="(contains(Name, 'Eggshell'))">
                            <span>Eggshell</span>
                            </xsl:when>
                            <xsl:when test="(contains(Name, 'Matte'))">
                            <span>Matte</span>
                            </xsl:when>
                            <xsl:otherwise>
                            </xsl:otherwise>
                        </xsl:choose>
                        <!-- End of repeated content, thanks in advance!!! -->
                                    </td>
                                </tr>
                            </table>
                </xsl:for-each>

        
        </xsl:if>

    </body>

    </html>

    </xsl:template>
</xsl:stylesheet>

编辑:
以下是 ShipWorks 提供的输入。输出应该是一个页面,中间有“Matte”或“Eggshell”

<?xml version="1.0" encoding="utf-8"?>
<ShipWorks>
  <Generated>2018-03-21T16:09:39.905698Z</Generated>
  <Timestamp>1521648579</Timestamp>
  <UniqueID></UniqueID>
  <Context>Order</Context>
  <Template>
    <Name>XML Source</Name>
    <Folder>System\Utility</Folder>
    <Output>
      <ContentWidth>7</ContentWidth>
      <ContentHeight>9.5</ContentHeight>
    </Output>
  </Template>
  <User ID="1002">
    <Username></Username>
    <IsAdmin>true</IsAdmin>
  </User>
  <Store ID="20005">
    <StoreName></StoreName>
    <StoreType ID="28">
      <Code>GENERIC</Code>
      <Name>Generic - Module</Name>
    </StoreType>
    <LastDownload>2018-03-21T16:05:50.933Z</LastDownload>
    <Address type="company">
      <Company></Company>
      <Line1></Line1>
      <Line2></Line2>
      <Line3></Line3>
      <City></City>
      <StateCode></StateCode>
      <StateName></StateName>
      <PostalCode></PostalCode>
      <CountryCode></CountryCode>
      <CountryName></CountryName>
      <Phone></Phone>
      <Fax></Fax>
      <Email></Email>
      <Website></Website>
      <AddressValidationStatus>Not Checked</AddressValidationStatus>
      <ResidentialStatus>Unknown</ResidentialStatus>
      <POBox>Unknown</POBox>
      <USTerritory>Unknown</USTerritory>
      <MilitaryAddress>Unknown</MilitaryAddress>
    </Address>
  </Store>
  <Customer ID="401765012">
    <Summary>
      <TotalSpent>80.4000</TotalSpent>
      <OrdersPlaced>1</OrdersPlaced>
    </Summary>
    <Address type="ship">
      <FullName></FullName>
      <FirstName></FirstName>
      <MiddleName></MiddleName>
      <LastName></LastName>
      <Company></Company>
      <Line1></Line1>
      <Line2></Line2>
      <Line3></Line3>
      <City></City>
      <StateCode></StateCode>
      <StateName></StateName>
      <PostalCode></PostalCode>
      <CountryCode></CountryCode>
      <CountryName></CountryName>
      <Phone></Phone>
      <Fax></Fax>
      <Email></Email>
      <Website></Website>
      <AddressValidationStatus>Not Checked</AddressValidationStatus>
      <ResidentialStatus>Unknown</ResidentialStatus>
      <POBox>Unknown</POBox>
      <USTerritory>Unknown</USTerritory>
      <MilitaryAddress>Unknown</MilitaryAddress>
    </Address>
    <Address type="bill">
      <FullName></FullName>
      <FirstName></FirstName>
      <MiddleName></MiddleName>
      <LastName></LastName>
      <Company></Company>
      <Line1></Line1>
      <Line2></Line2>
      <Line3></Line3>
      <City></City>
      <StateCode></StateCode>
      <StateName></StateName>
      <PostalCode></PostalCode>
      <CountryCode></CountryCode>
      <CountryName></CountryName>
      <Phone></Phone>
      <Fax></Fax>
      <Email></Email>
      <Website></Website>
      <AddressValidationStatus>Not Checked</AddressValidationStatus>
      <ResidentialStatus>Unknown</ResidentialStatus>
      <POBox>Unknown</POBox>
      <USTerritory>Unknown</USTerritory>
      <MilitaryAddress>Unknown</MilitaryAddress>
    </Address>
    <Order ID="554407006" storeID="20005">
      <Number>71704</Number>
      <Date>2018-01-19T00:10:28Z</Date>
      <Status>Complete</Status>
      <OnlineStatus>Complete</OnlineStatus>
      <OnlineStatusCode>C</OnlineStatusCode>
      <OnlineCustomerID>11886</OnlineCustomerID>
      <IsManual>false</IsManual>
      <RequestedShipping>UPS Ground (Signature Required)</RequestedShipping>
      <Total>80.4000</Total>
      <Address type="ship">
        <FullName></FullName>
        <FirstName></FirstName>
        <MiddleName></MiddleName>
        <LastName></LastName>
        <Company></Company>
        <Line1></Line1>
        <Line2></Line2>
        <Line3></Line3>
        <City></City>
        <StateCode></StateCode>
        <StateName></StateName>
        <PostalCode></PostalCode>
        <CountryCode></CountryCode>
        <CountryName></CountryName>
        <Phone></Phone>
        <Fax></Fax>
        <Email></Email>
        <Website></Website>
        <AddressValidationStatus>Fixed</AddressValidationStatus>
        <ResidentialStatus>Yes</ResidentialStatus>
        <POBox>No</POBox>
        <USTerritory>No</USTerritory>
        <MilitaryAddress>No</MilitaryAddress>
      </Address>
      <Address type="bill">
        <FullName></FullName>
        <FirstName></FirstName>
        <MiddleName></MiddleName>
        <LastName></LastName>
        <Company></Company>
        <Line1></Line1>
        <Line2></Line2>
        <Line3></Line3>
        <City></City>
        <StateCode></StateCode>
        <StateName></StateName>
        <PostalCode></PostalCode>
        <CountryCode></CountryCode>
        <CountryName></CountryName>
        <Phone></Phone>
        <Fax></Fax>
        <Email></Email>
        <Website></Website>
        <AddressValidationStatus>Fixed</AddressValidationStatus>
        <ResidentialStatus>Yes</ResidentialStatus>
        <POBox>No</POBox>
        <USTerritory>No</USTerritory>
        <MilitaryAddress>No</MilitaryAddress>
      </Address>
      <Item ID="540525013">
        <Name>Gloss Varnish</Name>
        <Code></Code>
        <SKU></SKU>
        <Description></Description>
        <Location></Location>
        <ISBN></ISBN>
        <UPC></UPC>
        <Thumbnail></Thumbnail>
        <Image></Image>
        <UnitPrice>29.7500</UnitPrice>
        <UnitPriceWithOptions>29.7500</UnitPriceWithOptions>
        <UnitCost>0</UnitCost>
        <Weight>0</Weight>
        <Quantity>1</Quantity>
        <TotalPrice>29.7500</TotalPrice>
        <TotalPriceWithOptions>29.7500</TotalPriceWithOptions>
        <TotalCost>0</TotalCost>
        <TotalWeight>0</TotalWeight>
        <Status></Status>
        <IsManual>false</IsManual>
        <HarmonizedCode></HarmonizedCode>
        <Option ID="623514020">
          <Name>Size</Name>
          <Description>1 Quart</Description>
          <UnitPrice>0</UnitPrice>
        </Option>
      </Item>
      <Item ID="540526013">
        <Name>Wood Stain</Name>
        <Code></Code>
        <SKU></SKU>
        <Description></Description>
        <Location></Location>
        <ISBN></ISBN>
        <UPC></UPC>
        <Thumbnail></Thumbnail>
        <Image></Image>
        <UnitPrice>20.3500</UnitPrice>
        <UnitPriceWithOptions>20.3500</UnitPriceWithOptions>
        <UnitCost>0</UnitCost>
        <Weight>0</Weight>
        <Quantity>1</Quantity>
        <TotalPrice>20.3500</TotalPrice>
        <TotalPriceWithOptions>20.3500</TotalPriceWithOptions>
        <TotalCost>0</TotalCost>
        <TotalWeight>0</TotalWeight>
        <Status></Status>
        <IsManual>false</IsManual>
        <HarmonizedCode></HarmonizedCode>
        <Option ID="623515020">
          <Name>Ordered this color before ?</Name>
          <Description>No</Description>
          <UnitPrice>0</UnitPrice>
        </Option>
        <Option ID="623516020">
          <Name>Size</Name>
          <Description>1 Gallon</Description>
          <UnitPrice>0</UnitPrice>
        </Option>
      </Item>
      <Item ID="540527013">
        <Name>Matte Interior Wall & Ceiling Paint</Name>
        <Code></Code>
        <SKU></SKU>
        <Description></Description>
        <Location></Location>
        <ISBN></ISBN>
        <UPC></UPC>
        <Thumbnail></Thumbnail>
        <Image></Image>
        <UnitPrice>20.3500</UnitPrice>
        <UnitPriceWithOptions>20.3500</UnitPriceWithOptions>
        <UnitCost>0</UnitCost>
        <Weight>0</Weight>
        <Quantity>1</Quantity>
        <TotalPrice>20.3500</TotalPrice>
        <TotalPriceWithOptions>20.3500</TotalPriceWithOptions>
        <TotalCost>0</TotalCost>
        <TotalWeight>0</TotalWeight>
        <Status></Status>
        <IsManual>false</IsManual>
        <HarmonizedCode></HarmonizedCode>
        <Option ID="623518020">
          <Name>Size</Name>
          <Description>1 Quart</Description>
          <UnitPrice>0</UnitPrice>
        </Option>
      </Item>
      <Charge ID="512146021">
        <Type>SHIPPING</Type>
        <Description>Shipping</Description>
        <Amount>9.9500</Amount>
      </Charge>
      <Payment />
      <Shipment ID="541189031">
        <ShipmentType code="0">UPS</ShipmentType>
        <Status>Processed</Status>
        <Processed>true</Processed>
        <ProcessedDate>2018-01-19T16:46:14.09Z</ProcessedDate>
        <ProcessedUser ID="8002">
          <Username></Username>
          <IsAdmin>false</IsAdmin>
        </ProcessedUser>
        <Voided>false</Voided>
        <VoidedDate></VoidedDate>
        <ShippedDate>2018-01-19T12:00:00Z</ShippedDate>
        <ServiceUsed>UPS Ground</ServiceUsed>
        <ReturnShipment>false</ReturnShipment>
        <TrackingNumber></TrackingNumber>
        <TotalCharges>16.3300</TotalCharges>
        <TotalWeight>9</TotalWeight>
        <Address type="ship">
          <FullName></FullName>
          <FirstName></FirstName>
          <MiddleName></MiddleName>
          <LastName></LastName>
          <Company></Company>
          <Line1></Line1>
          <Line2></Line2>
          <Line3></Line3>
          <City></City>
          <StateCode></StateCode>
          <StateName></StateName>
          <PostalCode></PostalCode>
          <CountryCode></CountryCode>
          <CountryName></CountryName>
          <Phone></Phone>
          <Fax></Fax>
          <Email></Email>
          <Website></Website>
          <AddressValidationStatus>Fixed</AddressValidationStatus>
          <ResidentialStatus>Yes</ResidentialStatus>
          <POBox>No</POBox>
          <USTerritory>No</USTerritory>
          <MilitaryAddress>No</MilitaryAddress>
        </Address>
        <Address type="from">
          <FullName></FullName>
          <FirstName></FirstName>
          <MiddleName></MiddleName>
          <LastName></LastName>
          <Company></Company>
          <Line1></Line1>
          <Line2></Line2>
          <Line3></Line3>
          <City></City>
          <StateCode></StateCode>
          <StateName></StateName>
          <PostalCode></PostalCode>
          <CountryCode></CountryCode>
          <CountryName></CountryName>
          <Phone></Phone>
          <Fax></Fax>
          <Email></Email>
          <Website></Website>
          <AddressValidationStatus>Not Checked</AddressValidationStatus>
          <ResidentialStatus>Unknown</ResidentialStatus>
          <POBox>Unknown</POBox>
          <USTerritory>Unknown</USTerritory>
          <MilitaryAddress>Unknown</MilitaryAddress>
        </Address>
        <BestRateEvent>None</BestRateEvent>
        <ShipSense>
          <Status>NotApplied</Status>
          <ChangeSets />
        </ShipSense>
        <BilledType>Actual weight</BilledType>
        <BilledWeight>9</BilledWeight>
        <Package ID="523087063" TrackingNumber="">
          <Insurance>
            <Provider>Carrier</Provider>
            <InsuredValue>100.0000</InsuredValue>
          </Insurance>
          <Dimensions>
            <Length>13</Length>
            <Width>13</Width>
            <Height>7.25</Height>
          </Dimensions>
          <TotalWeight>9</TotalWeight>
        </Package>
        <Labels type="thermal">
          <Package ID="523087063">
            <Primary>
              <Label name="Label" thermal="ZPL"></Label>
            </Primary>
          </Package>
        </Labels>
        <RequestedLabelFormat>Thermal - ZPL</RequestedLabelFormat>
      </Shipment>
    </Order>
  </Customer>
</ShipWorks>

最佳答案

使用 XSLT 2.0(或更高版本)此任务会更容易,因为您可以使用 range expression 创建数字序列。即 (1 to Quantity) 进行迭代。

对于 XSLT 1.0,一种方法是使用递归模板调用,其中您在一个参数中传递要重复的内容,并使用另一个参数来指示重复次数。每次调用该模板时,产生所需的内容,然后递减数量并再次调用该模板,直到重复次数参数等于0。

将此模板添加到您的样式表中:

<xsl:template name="repeat">
    <xsl:param name="content"/>
    <xsl:param name="times"/>
    <xsl:if test="$times > 0">
        <xsl:copy-of select="$content"/>
        <xsl:call-template name="repeat">
            <xsl:with-param name="content" select="$content"/>
            <xsl:with-param name="times" select="$times - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

您可以调整对 Item 元素的处理,以生成 Item/Name 的内容,并根据 Quantity< 重复多次。/值:

<xsl:for-each select="$order/Item">                      
  <table style="width: 100%; height: 100%;" cellspacing="0">
    <tr>              
        <!-- this is what needs to be repeated based on the value of $order/Item/Quantity -->
         <xsl:variable name="name">
           <td style="{$orderDetailAttributeStyle}; width: 100%; height: 100%; vertical-align: middle;" align="center"> 
             <xsl:choose>
               <xsl:when test="(contains(Name, 'Eggshell'))">
                 <span>Eggshell</span>
               </xsl:when>
               <xsl:when test="(contains(Name, 'Matte'))">
                 <span>Matte</span>
               </xsl:when>
               <xsl:otherwise/>
            </xsl:choose>
          </td>
        </xsl:variable>
        <xsl:call-template name="repeat">
          <xsl:with-param name="content" select="$name"/>
          <xsl:with-param name="times" select="number(Quantity)"/>
        </xsl:call-template>
        <!-- End of repeated content, thanks in advance!!! -->  
    </tr>
  </table>
</xsl:for-each>

关于xslt - XSL 新手,需要一些建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49407184/

相关文章:

xml - XSLT 能做 CSS 能做的一切吗?

jquery - 使用 jQuery 更新 XML 文档并执行 XSLT

xslt - 如何在 .NET 4.0 中使用 XPath 2.0 方法?

html - 使用服务器端 XML 解析器生成 HTML 内容

xml - XSLT - 如何选择顶部 a 到顶部 b

xslt - 从 XML 中排除命名空间

php - 在 XSL 中处理自定义 XML 命名空间

xml - 添加额外的 xmlns :xsi attributes to XML element

xml - 创建后在 xsl 中对表进行排序

javascript - 使用 JavaScript/XLS 函数在 XML 中包含命名空间