如何在XSLT中添加Closing标签

时间:2015-03-25 15:52:52

标签: xslt

我需要你的逻辑来帮助添加结束标记。我正在看的结构是。我尝试使用for-each或xsl:if或xsl:choose。输入XML如下所示

   <SuperShipNotice>
   <Package packageType="P" packageLevel="1">
   <PackageNumber>PWN34332</PackageNumber>
   <ShipmentNumber>105909390</ShipmentNumber>
   <ShipmentLineNumber>1</ShipmentLineNumber>
   <PartNumber>1CH162-510</PartNumber>
   <Quantity>1000</Quantity>
   <SSCCNumber>00176364909402100165</SSCCNumber>
   </Package>
   <Package packageType="C" packageLevel="2">
   <PackageNumber>CWX612432660</PackageNumber>
   <ParentPackageNumber>PWN34332</ParentPackageNumber>
   <ShipmentNumber>105909390</ShipmentNumber>
   <ShipmentLineNumber>1</ShipmentLineNumber>
   <PartNumber>1CH162-510</PartNumber>
   <Quantity>25</Quantity>
   <SSCCNumber>00176364909402100165</SSCCNumber>
   </Package>
   <Package packageType="S" packageLevel="3">
   <PackageNumber>W1D2WNGL</PackageNumber>
   <ParentPackageNumber>CWX612432660</ParentPackageNumber>
   <ShipmentNumber>105909390</ShipmentNumber>
   <ShipmentLineNumber>1</ShipmentLineNumber>
   <PartNumber>1CH162-510</PartNumber>
   <Quantity>1</Quantity>
   <DateOfMfg>20131209</DateOfMfg>
   <COO>CN</COO>
   <SSCCNumber>00176364909402100165</SSCCNumber>
   </Package>
   <Package packageType="S" packageLevel="3">
      <PackageNumber>W1D2WNGL</PackageNumber>
      <ParentPackageNumber>CWX612432660</ParentPackageNumber>
      <ShipmentNumber>105909390</ShipmentNumber>
      <ShipmentLineNumber>1</ShipmentLineNumber>
      <PartNumber>1CH162-510</PartNumber>
      <Quantity>1</Quantity>
      <DateOfMfg>20131209</DateOfMfg>
      <COO>CN</COO>
      <SSCCNumber>00176364909402100165</SSCCNumber>
      </Package>
     </SuperShipNotice>

1 个答案:

答案 0 :(得分:0)

不确定这是否对您有用 - 遵循XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
  <xsl:template match="SuperShipNotice">
    <xsl:apply-templates select="//PackageNumber[parent::Package[@packageLevel='1']]" />
</xsl:template>
<xsl:template match="PackageNumber[parent::Package[@packageLevel='1']]">
    <xsl:variable name="packageNumber" select="." />
    <PkgLevel1>
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
        <xsl:apply-templates select="//PackageNumber[parent::Package[@packageLevel='2'] and parent::Package/ParentPackageNumber = $packageNumber]" />
    </PkgLevel1>
</xsl:template>
<xsl:template match="PackageNumber[parent::Package[@packageLevel='2']]">
    <xsl:variable name="packageNumber" select="." />
    <PkgLevel2>
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
        <xsl:apply-templates select="//PackageNumber[parent::Package[@packageLevel='3'] and parent::Package/ParentPackageNumber = $packageNumber
and not(parent::Package/ParentPackageNumber = preceding::Package[@packageLevel='3']/ParentPackageNumber)]" />
    </PkgLevel2>
  </xsl:template>
  <xsl:template match="PackageNumber[parent::Package[@packageLevel='3']]">
    <PkgLevel3>
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </PkgLevel3>
  </xsl:template>
</xsl:transform>

当应用于您的输入时,XML会生成输出

<PkgLevel1>
  <PackageNumber>PWN34332</PackageNumber>
  <PkgLevel2>
    <PackageNumber>CWX612432660</PackageNumber>
    <PkgLevel3>
       <PackageNumber>W1D2WNGL</PackageNumber>
    </PkgLevel3>
  </PkgLevel2>
</PkgLevel1>

第一个匹配SuperShipNotice的模板将模板应用于PackageNumber个值为Package packageLevel的{​​{1}}个。{ 在模板匹配中,这些1模板将应用于PackagenumberPackageNumber和当前packageLevel的{​​{1}}的所有2
由于ParentPackageNumber值为PackageNumber的{​​{1}}有双重条目,因此省略了具有相同Packages的第二个packageLevel

3

如果您可以根据需要进行调整,则可以使用已保存的Demo