繁体   English   中英

使用XSLT合并两个XML文件

[英]Merge two XML files with XSLT

我需要合并两个XML文件。

我尝试根据使用XSLT的基于属性值的Merge 2 XML文件中的答案编写XSLT样式表 ,但我没有成功。

_a1.xml

<?xml version="1.0" encoding="UTF-8"?>
<ExtData>
  <table bName="B SERs" id="BSER">
    <Col bName="Bus" id="BUS">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="Bus" coreId="BUS"/>
    </Col>
    <Col bName="Ser" id="NAME">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="Ser" coreId="NAME"/>
    </Col>
    <Col bName="ID" id="ID">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="SerId" coreId="UCMDB_ID"/>
    </Col>
  </table>
</ExtData>

_a2.xml

<?xml version="1.0" encoding="UTF-8"?>
<ExtData>
  <table bName="B SERs" id="BSER">
    <Col EName="SER" bName="Bus" Id="BUS"/>
    <Col EName="SER" bName="Ser" Id="NAME"/>
    <Col EName="SER" bName="SerId" Id="DB_ID"/>
    <Col EName="SER" bName="SerDate" Id="date"/>
    <Col EName="SER" bName="Person" Id="Manager"/>
    <Col EName="SER" bName="desc" Id="desc"/>
  </table>
</ExtData>

输出需要是:

<?xml version="1.0" encoding="UTF-8"?>
<ExtData>
  <table bName="B SERs" id="BSER">
    <Col bName="Bus" id="BUS">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="Bus" coreId="BUS"/>
    </Col>
    <Col bName="Ser" id="NAME">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="Ser" coreId="NAME"/>
    </Col>
    <Col bName="ID" id="ID">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="SerId" coreId="UCMDB_ID"/>
    </Col>
    <Col bName="" id="">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="SerDate" coreId="date"/>
    </Col>
    <Col bName="" id="">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="Person" coreId="Manager"/>
    </Col>
    <Col bName="" id="">
      <CoreCol coreEName="SER" coreHref="../_a2.xml" corebName="desc" coreId="desc"/>
    </Col>
  </table>
</ExtData>

可以使用XSLT做类似的事情吗?

我敢肯定有更好的方法,但这是我想到的第一个与XSLT 1.0兼容的选项:

样式表

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" 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="table">
    <xsl:copy>
      <xsl:apply-templates select="@* | Col"/>
      <!--
      Apply <Col> elements in _a2.xml that have a @bName attribute that DOESN'T
      have a <CoreCol> element under the current <table> element with a
      corresponding @corebName attribute.
      -->
      <xsl:apply-templates
        select="document('_a2.xml')/ExtData/table[@id = current()/@id]
         /Col[not(current()/Col/CoreCol/@corebName = @bName)]" mode="merge"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="table/@bName">
    <xsl:copy/>
  </xsl:template>

  <!-- Match the elements applied in the template above. -->
  <xsl:template match="Col" mode="merge">
    <Col bName="" id="">
      <CoreCol>
        <xsl:apply-templates select="@EName"/>
        <xsl:attribute name="coreHref">../_a2.xml</xsl:attribute>
        <xsl:apply-templates select="@bName" mode="merge"/>
        <xsl:apply-templates select="@Id"/>
      </CoreCol>
    </Col>
  </xsl:template>

  <!-- Transform attribute names -->
  <xsl:template match="@EName">
    <xsl:attribute name="coreEName">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <!--
  The @bName attributes in <Col> elements in _a2.xml need to be transformed
  into @coreBname elements in the output file. We'll use the "merge" mode so
  the @bName attributes in <Col> elements in _a1.xml aren't affected.
  -->
  <xsl:template match="@bName" mode="merge">
    <xsl:attribute name="corebName">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@Id">
    <xsl:attribute name="coreId">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM