我有一个XML:
<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<AddedBOMs NAME="AddedBOMs" TYPE="Unknown" STATUS="0">
<BOMHeader>
<Number>One</Number>
<StartEffectivity>One 00:00:00</StartEffectivity>
<EndEffectivity>One 00:00:00</EndEffectivity>
</BOMHeader>
</AddedBOMs>
<ChangedBOMs NAME="ChangedBOMs" TYPE="Unknown" STATUS="0">
<BOMHeader>
<Number>Two</Number>
<StartEffectivity>Two 00:00:00</StartEffectivity>
<EndEffectivity>Two 00:00:00</EndEffectivity>
</BOMHeader>
</ChangedBOMs>
<UnchangedBOMs NAME="UnchangedBOMs" TYPE="Unknown" STATUS="0">
<BOMHeader>
<Number>Three</Number>
<StartEffectivity>Three 00:00:00</StartEffectivity>
<EndEffectivity>Three 00:00:00</EndEffectivity>
</BOMHeader>
</UnchangedBOMs>
<DeletedBOMComponents NAME="DeletedBOMComponents" TYPE="Unknown" STATUS="0">
<BOMComponent>
<PartNumber>Three</PartNumber>
</BOMComponent>
</DeletedBOMComponents>
<AddedBOMComponents NAME="AddedBOMComponents" TYPE="Unknown" STATUS="0">
<BOMComponent>
<PartNumber>One</PartNumber>
</BOMComponent>
</AddedBOMComponents>
<ChangedBOMComponents NAME="ChangedBOMComponents" TYPE="Unknown" STATUS="0">
<BOMComponent>
<PartNumber>Two</PartNumber>
</BOMComponent>
</ChangedBOMComponents>
</COLLECTION>
预期输出如下。
<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<AddedBOMs NAME="AddedBOMs" TYPE="Unknown" STATUS="0">
<BOMHeader>
<Number>One</Number>
<StartEffectivity>One 00:00:00</StartEffectivity>
<EndEffectivity>One 00:00:00</EndEffectivity>
</BOMHeader>
</AddedBOMs>
<ChangedBOMs NAME="ChangedBOMs" TYPE="Unknown" STATUS="0">
<BOMHeader>
<Number>Two</Number>
<StartEffectivity>Two 00:00:00</StartEffectivity>
<EndEffectivity>Two 00:00:00</EndEffectivity>
</BOMHeader>
</ChangedBOMs>
<UnchangedBOMs NAME="UnchangedBOMs" TYPE="Unknown" STATUS="0">
<BOMHeader>
<Number>Three</Number>
<StartEffectivity>Three 00:00:00</StartEffectivity>
<EndEffectivity>Three 00:00:00</EndEffectivity>
</BOMHeader>
</UnchangedBOMs>
<DeletedBOMComponents NAME="DeletedBOMComponents" TYPE="Unknown" STATUS="0">
<BOMComponent>
<PartNumber>Three</PartNumber>
<StartEffectivity>--</StartEffectivity>
<EndEffectivity>Three 00:00:00</EndEffectivity>
</BOMComponent>
</DeletedBOMComponents>
<AddedBOMComponents NAME="AddedBOMComponents" TYPE="Unknown" STATUS="0">
<BOMComponent>
<PartNumber>One</PartNumber>
<StartEffectivity>One 00:00:00</StartEffectivity>
<EndEffectivity>One 00:00:00</EndEffectivity>
</BOMComponent>
</AddedBOMComponents>
<ChangedBOMComponents NAME="ChangedBOMComponents" TYPE="Unknown" STATUS="0">
<BOMComponent>
<PartNumber>Two</PartNumber>
<StartEffectivity>Two 00:00:00</StartEffectivity>
<EndEffectivity>Two 00:00:00</EndEffectivity>
</BOMComponent>
</ChangedBOMComponents>
</COLLECTION>
这是不固定的, addedbomms 将移动到AddedBOMComponents等等。它依赖于PartNumber从BOMComponent和Number从BOMHeader,匹配它的 starteffectip 和 endffectip 需要移动。
棘手的事情是在DeletedBOMComponents/BOMComponentStartEffectivity在BOMComponent需要保持为空和StartEffectivity从BOMHeader将成为EndEffectivity在BOMComponent
为此,我制作了一个XSL文件,但它不起作用。
<xsl:stylesheet version= "1.0"
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= "xml" version= "1.0" encoding= "UTF-8" indent= "yes" />
<xsl:strip-space elements= "*" />
<xsl:template match= "@*|node()" >
<xsl:copy>
<xsl:apply-templates select= "@*|node()" />
</xsl:copy>
</xsl:template >
<xsl:template match="BOMComponent/PartNumber">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
<start><xsl:apply-templates
select="../../BOMHeader[Number = current()]/StartEffectivity" mode="move"/></start>
<end><xsl:apply-templates
select="../../BOMHeader[Number = current()]/EndEffectivity" mode="move"/></end>
</xsl:template>
</xsl:stylesheet>
请建议:
AFAICT,你想做:
XSLT 1.0<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="header" match="BOMHeader" use="Number" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="BOMComponent">
<xsl:variable name="head" select="key('header', PartNumber)" />
<xsl:copy>
<xsl:copy-of select="PartNumber"/>
<xsl:copy-of select="$head/StartEffectivity | $head/EndEffectivity"/>
</xsl:copy>
</xsl:template>
<xsl:template match="DeletedBOMComponents/BOMComponent">
<xsl:copy>
<xsl:copy-of select="PartNumber"/>
<StartEffectivity>--</StartEffectivity>
<EndEffectivity>
<xsl:value-of select="key('header', PartNumber)/StartEffectivity" />
</EndEffectivity>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>