XSL新手,需要一些建议

时间:2018-03-21 13:08:24

标签: xslt

StackOverflow的长期潜伏,但第一次发布。我通常能够从你这些优秀的人那里找到我正在寻找的东西,而且从来不需要发表我自己的问题。

但是这个有点不同,因为我对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>

2 个答案:

答案 0 :(得分:1)

正如Tim的建议,很高兴看到XML示例,但据我从代码中注意到,当你为&#34; order&#34;定义变量时。你只选择它的第一个值。

 <xsl:variable name="order" select="Customer/Order[1]" />

这将选择第一个Order值,当你选择for-each $ order时,它只会给你一个值,而不是所有的值,因为你用[1]限制了它,因此,得到它是正常的逻辑上只有一个值。

答案 1 :(得分:1)

使用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>