从文件中提取元素的内容,并根据键匹配将其附加到输入文件xslt

时间:2017-05-18 15:30:12

标签: xml xslt xslt-2.0

文件a.xml:

<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="pivot.cs">
   <DATA RECORDS="2">
      <RECORD ID="1">
         <INTERNALID>5510</INTERNALID>
         <SOMED>1</SOMED>
         <PEMED>1</PEMED>
         <CODAL>PLACEHOLD</CODAL>
      </RECORD>
      <RECORD ID="2">
         <INTERNALID>5511</INTERNALID>
         <SOMED>1</SOMED>
         <PEMED>1</PEMED>
         <CODAL>PLACEHOLD</CODAL>
      </RECORD>
      <RECORD ID="3">
      <INTERNALID>5537</INTERNALID>
      <SOMED>2</SOMED>
      <PEMED>1</PEMED>
      <CODAL>PLACEHOLD</CODAL>
      </RECORD>
   </DATA>
</TABLE>

file b.xml:

  <?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="ALT.CS">
   <DATA RECORDS="20">
      <RECORD ID="53">
         <RECNO>5510</RECNO>
         <TOBEEXTRACTED>TIM</TOBEEXTRACTED>
      </RECORD>
      <RECORD ID="53">
         <RECNO>5510</RECNO>
         <TOBEEXTRACTED>KLM</TOBEEXTRACTED>
      </RECORD>
      <RECORD ID="54">
         <RECNO>5510</RECNO>
         <TOBEEXTRACTED>KAB</TOBEEXTRACTED>
      </RECORD>
      <RECORD ID="55">
         <RECNO>5511</RECNO>
         <TOBEEXTRACTED>BUS WEE</TOBEEXTRACTED>
      </RECORD>
         <RECORD ID="5511">
            <RECNO>5537</RECNO>
            <TOBEEXTRACTED>FBV</TOBEEXTRACTED>
         </RECORD>
      </RECORD>
   </DATA>
</TABLE>

我需要将文件b.xml中的信息加入文件a。 xml,其中INTERNALID = RECNO,以便将当前数据保存在元素CODAL中, 但附加了TOBEEXTRACTED元素的内容。 输出文件应该是文件a.xml,但是在[]中附加了TOBEEXTRACTED元素文本以及一个字母, 表示SOMEID号码: 1 - &gt; A,2 - &gt; B,3 - &gt; C等......

所以输出将变为:

<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="pivot.cs">
   <DATA RECORDS="2">
      <RECORD ID="1">
         <INTERNALID>5599</INTERNALID>
         <SOMED>1</SOMED>
         <PEMED>1</PEMED>
         <CODAL>PLACEHOLD</CODAL>
      </RECORD>
      <RECORD ID="2">
         <INTERNALID>5511</INTERNALID>
         <SOMED>1</SOMED>
         <PEMED>1</PEMED>
         <CODAL>PLACEHOLD [BUS WEE A]</CODAL>
      </RECORD>
      <RECORD ID="3">
      <INTERNALID>5537</INTERNALID>
      <SOMED>2</SOMED>
      <PEMED>1</PEMED>
      <CODAL>PLACEHOLD [FBV B]</CODAL>
      </RECORD>
   </DATA>
</TABLE>

1 个答案:

答案 0 :(得分:0)

您的示例不会加起来。假设您正在处理以下输入:

<强> XML

<TABLE NAME="pivot.cs">
  <DATA RECORDS="3">
    <RECORD ID="1">
      <INTERNALID>5550</INTERNALID>
      <SOMEID>1</SOMEID>
      <CODAL>ZERO</CODAL>
    </RECORD>
    <RECORD ID="2">
      <INTERNALID>5556</INTERNALID>
      <SOMEID>2</SOMEID>
      <CODAL>SIX</CODAL>
    </RECORD>
    <RECORD ID="3">
      <INTERNALID>5557</INTERNALID>
      <SOMEID>3</SOMEID>
      <CODAL>SEVEN</CODAL>
    </RECORD>
  </DATA>
</TABLE>

并将路径传递给另一个XML文件:

<强> B.XML

<TABLE NAME="ALT.CS">
  <DATA RECORDS="5">
    <RECORD ID="15">
      <RECNO>5555</RECNO>
      <TOBEEXTRACTED>ECHO</TOBEEXTRACTED>
    </RECORD>
    <RECORD ID="16">
      <RECNO>5556</RECNO>
      <TOBEEXTRACTED>FOXTROT</TOBEEXTRACTED>
    </RECORD>
    <RECORD ID="17">
      <RECNO>5557</RECNO>
      <TOBEEXTRACTED>GOLF</TOBEEXTRACTED>
    </RECORD>
  </DATA>
</TABLE>

作为参数,您可以使用:

XSLT 2.0

<xsl:stylesheet version="2.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:param name="lookup-path">path/to/b.xml</xsl:param>
<xsl:key name="lookup" match="RECORD" use="RECNO" />

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

<xsl:template match="CODAL">
    <xsl:variable name="match" select="key('lookup', ../INTERNALID, document($lookup-path))" />
    <xsl:copy>
        <xsl:value-of select="."/>
        <xsl:if test="$match">
            <xsl:text> [</xsl:text>
            <xsl:value-of select="$match/TOBEEXTRACTED"/>
            <xsl:text> </xsl:text>
            <xsl:number value="../SOMEID" format="A" />
            <xsl:text>]</xsl:text>
        </xsl:if>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

返回:

<强>结果

<TABLE NAME="pivot.cs">
  <DATA RECORDS="3">
    <RECORD ID="1">
      <INTERNALID>5550</INTERNALID>
      <SOMEID>1</SOMEID>
      <CODAL>ZERO</CODAL>
    </RECORD>
    <RECORD ID="2">
      <INTERNALID>5556</INTERNALID>
      <SOMEID>2</SOMEID>
      <CODAL>SIX [FOXTROT B]</CODAL>
    </RECORD>
    <RECORD ID="3">
      <INTERNALID>5557</INTERNALID>
      <SOMEID>3</SOMEID>
      <CODAL>SEVEN [GOLF C]</CODAL>
    </RECORD>
  </DATA>
</TABLE>

警告:未经测试

加了:

要在XSLT 1.0中完成同样的事情,你必须这样做:

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:param name="lookup-path">b.xml</xsl:param>
<xsl:key name="lookup" match="RECORD" use="RECNO" />

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

<xsl:template match="CODAL">
    <xsl:copy>
        <xsl:value-of select="."/>
        <!-- place values from this document into variables -->
        <xsl:variable name="internal-id" select="../INTERNALID" />
        <xsl:variable name="some-id" select="../SOMEID" />
        <!-- switch context to the other document -->
        <xsl:for-each select="document($lookup-path)">
            <xsl:variable name="match" select="key('lookup', $internal-id)" />
            <xsl:if test="$match">
                <xsl:text> [</xsl:text>
                <xsl:value-of select="$match/TOBEEXTRACTED"/>
                <xsl:text> </xsl:text>
                <xsl:number value="$some-id" format="A" />
                <xsl:text>]</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>