如何使用xslt要求对Xml元素执行重命名和移动操作

时间:2016-07-29 03:51:48

标签: xml xslt

我有一个xml说 INPUT.xml

<paran>
some more attributes
    </paran>
    <child1>
some more attributes
    </child1>
    <child2>
some more attributes
    </child2>

我想以这样的方式对它执行xslt,它会生成

<Parent>
some more attributes
<child>
    some more attributes
 </child>
 <child>
    some more attributes
 </child>
</Parent>

正如您所看到的,我想重命名元素并移动它。我怎么能为它写一个xsl

XSLT

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="child1">
  <Child>
    <xsl:apply-templates select="@*|node()"/>
  </Child>
</xsl:template>
<xsl:template match="child2">
  <Child>
    <xsl:apply-templates select="@*|node()"/>
  </Child>
</xsl:template>
<xsl:template match="paran">
  <Parents>
    <xsl:apply-templates select="@*|node()"/>
  </Parents>
</xsl:template>
<xsl:template match="Parents">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
 </xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我认为你可能有点抽象了这个例子。如果我采用你前面的例子:

<强> XML

<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
   <Release NAME="Release" TYPE="Unknown" STATUS="0">
      <Transaction>
         <TransactionNumber>17</TransactionNumber>
         <ReleaseNumber>17</ReleaseNumber>
      </Transaction>
   </Release>
   <BOMHeader>
      <ObjectID>OR:wt.part.WTPart:401060081:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
   </BOMHeader>
   <ABC NAME="DeletedBOMComponents" TYPE="Unknown" STATUS="0">
      <ObjectID>OR:wt.part.WTPartMaster:401058642:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
   </ABC>
   <EFG NAME="AddedBOMComponents" TYPE="Unknown" STATUS="0">
      <ObjectID>OR:wt.part.WTPartMaster:401058642:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
   </EFG>
   <PQR NAME="ChangedBOMComponents" TYPE="Unknown" STATUS="0">
      <ObjectID>OR:wt.part.WTPartMaster:401058642:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
   </PQR>
   <BOMComponent/>
</COLLECTION>

并应用以下样式表:

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="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="BOMComponent">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <!-- move nodes to here -->
        <xsl:apply-templates select="../ABC | ../EFG | ../PQR" mode="move"/>
    </xsl:copy>
</xsl:template>

<!-- remove nodes from their original place -->
<xsl:template match="ABC | EFG | PQR" />

<!-- rename moved nodes (3x) -->
<xsl:template match="ABC" mode="move">
    <new-ABC>
        <xsl:apply-templates select="@*|node()"/>
    </new-ABC>
</xsl:template>

<xsl:template match="EFG" mode="move">
    <new-EFG>
        <xsl:apply-templates select="@*|node()"/>
    </new-EFG>
</xsl:template>

<xsl:template match="PQR" mode="move">
    <new-PQR>
        <xsl:apply-templates select="@*|node()"/>
    </new-PQR>
</xsl:template>

</xsl:stylesheet>

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
   <Release NAME="Release" TYPE="Unknown" STATUS="0">
      <Transaction>
         <TransactionNumber>17</TransactionNumber>
         <ReleaseNumber>17</ReleaseNumber>
      </Transaction>
   </Release>
   <BOMHeader>
      <ObjectID>OR:wt.part.WTPart:401060081:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
   </BOMHeader>
   <BOMComponent>
      <new-ABC NAME="DeletedBOMComponents" TYPE="Unknown" STATUS="0">
         <ObjectID>OR:wt.part.WTPartMaster:401058642:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
      </new-ABC>
      <new-EFG NAME="AddedBOMComponents" TYPE="Unknown" STATUS="0">
         <ObjectID>OR:wt.part.WTPartMaster:401058642:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
      </new-EFG>
      <new-PQR NAME="ChangedBOMComponents" TYPE="Unknown" STATUS="0">
         <ObjectID>OR:wt.part.WTPartMaster:401058642:104375456-1153348282062-33238777-27-83-253-132@icenterv01.ptc.com</ObjectID>
      </new-PQR>
   </BOMComponent>
</COLLECTION>
相关问题