0

我正在尝试规范化一段 xml。我在与ProductID. 我正在尝试为它创建一个容器,但<products><productid>xyz</productid></products><xsl:for-each select="ProductID"> 似乎不起作用。谁能指出我正确的方向?

<Batch>
  <Promotion>
    <PromotionID>000873</PromotionID>
    <Description country="GB" language="en" variant="">*P* Free Clotted Cream      Scone</Description>
    <MultibuyGroup>
        <RewardType>1</RewardType>
        <RewardValue>0.0</RewardValue>
        <RewardValue currency="GBP">1.0</RewardValue>
        <ProductID>1300003536</ProductID>
        <ProductID>1300000746</ProductID>
        <ProductID>1300002393</ProductID>
        <ProductID>1300002648</ProductID>
        <ProductID>1300002899</ProductID>
        <ProductID>1300003535</ProductID>
        <ProductID>1300003222</ProductID>
    </MultibuyGroup>
    <MultibuyGroup>
        <RewardType>0</RewardType><RewardValue>0.0</RewardValue>
        <RewardValue currency="GBP">0.0</RewardValue>
        <ProductID>1570000102</ProductID>
    </MultibuyGroup>
    <Timetable>
      <SartDate>2008-02-02T00:00:00+00:00</StartDate>
      <FinishDate>2008-02-03T23:59:59+00:00</FinishDate>
    </Timetable>
 </Promotion>
</Batch>

我试图让它看起来像。

<Batch>
  <Promotion>
    <PromotionID>000873</PromotionID>
    <badge_id>tbc</badge_id>
    <loyaltyaccountholdersonly>TBC</loyaltyaccountholdersonly>
    <locations>TBC</locations>
    <Description country="GB" language="en" variant="">*P* Free Clotted Cream      Scone</Description>
    <MultibuyGroup>
        <RewardType>1</RewardType>
        <RewardValue>0.0</RewardValue>
        <RewardValue currency="GBP">1.0</RewardValue>
        <products>
          <ProductID>1300003536</ProductID>
          <ProductID>1300000746</ProductID>
          <ProductID>1300002393</ProductID>
          <ProductID>1300002648</ProductID>
          <ProductID>1300002899</ProductID>
          <ProductID>1300003535</ProductID>
          <ProductID>1300003222</ProductID>
        </products>
    </MultibuyGroup>
    <MultibuyGroup>
        <RewardType>0</RewardType><RewardValue>0.0</RewardValue>
        <RewardValue currency="GBP">0.0</RewardValue>
        <ProductID>1570000102</ProductID>
    </MultibuyGroup>
    <Timetable>
      <SartDate>2008-02-02T00:00:00+00:00</StartDate>
      <FinishDate>2008-02-03T23:59:59+00:00</FinishDate>
    </Timetable>
 </Promotion>
</Batch>

使用以下 XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">  
<promotions> 
<xsl:for-each select="Batch/Promotion">  
  <promotion>
    <promotion_id><xsl:value-of select="PromotionID"/></promotion_id>    
    <badge_id>TBC</badge_id>
    <loyaltyaccountholdersonly>TBC</loyaltyaccountholdersonly>
    <description><xsl:value-of select="Description[@country = 'GB']"/></description>
    <locations>TBC</locations>
    <xsl:for-each select="MultibuyGroup">
      <multibuygroup>
        <products>
        <xsl:for-each select="ProductID">
          <product_id><xsl:value-of select="ProductID"/></product_id>
        </xsl:for-each>
        </products>
        <rewardtype><xsl:value-of select="RewardType"/></rewardtype> 
        <rewardvalue><xsl:value-of select="RewardValue"/></rewardvalue>
        <rewardthreshold>TBC</rewardthreshold>
        <groupdescription><xsl:value-of select="GroupDescription"/></groupdescription>
      </multibuygroup>
    </xsl:for-each>
    <timetable> 
      <startdate><xsl:value-of select="Timetable/StartDate"/></startdate>
      <expirydate><xsl:value-of select="Timetable/FinishDate"/></expirydate>
    </timetable>
  </promotion>
</xsl:for-each>   
</promotions>
</xsl:template>
</xsl:stylesheet>
4

1 回答 1

0

您的 XML 不正确(元素打开 SarDate 而不是 StartDate)。在您的尝试中,在我看来您忘记了 XML 区分大小写。如果您想在您的输出中有一个 PromotionID,那么如果您将promotion_id 作为XSLT 中的元素名称,它就不会发生。

反正。

这是一个 XSLT,它可以满足我的要求:

  1. 它首先要求复制所有元素,包括属性。
  2. 然后它要求向每个 Promotion 元素添加一些元素。
  3. 然后对于 Multibuy 的东西,它要求复制产品列表。
  4. 最后,它要求不要(再次)将 ProductID 元素复制到其原始位置。

伊夫

<?xml-stylesheet type="text/xsl"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*">  
    <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>
<xsl:template match="Promotion">  
    <xsl:copy>
        <badge_id>tbc</badge_id>
        <loyaltyaccountholdersonly>TBC</loyaltyaccountholdersonly>
        <locations>TBC</locations>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>
<xsl:template match="MultibuyGroup">  
    <xsl:copy>
        <xsl:copy-of select="@*" />

        <xsl:apply-templates />
        <Products>
            <xsl:for-each select="ProductID">
                <xsl:copy>
                    <xsl:copy-of select="@*" />
                    <xsl:apply-templates />
                </xsl:copy>
            </xsl:for-each>
        </Products>
    </xsl:copy>
</xsl:template>   
<xsl:template match="ProductID"></xsl:template>
</xsl:stylesheet>
于 2012-07-02T11:26:22.823 回答