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>
      <INTERNALID>5537</INTERNALID>
      <SOMED>1</SOMED>
      <PEMED>1</PEMED>
      <CODAL>PLACEHOLD</CODAL>
   </DATA>
</TABLE>

文件 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="59">
         <RECNO>5512</RECNO>
      </RECORD>
      <RECORD ID="60">
         <RECNO>5513</RECNO>
         </RECORD>
         <RECORD ID="5511">
            <RECNO>5598</RECNO>
            <TOBEEXTRACTED>FBV</TOBEEXTRACTED>
         </RECORD>
      </RECORD>
   </DATA>
</TABLE>

并且输出文件应该是文件 a.xml,但如果匹配一两次,则将 TOBEEXTRACTED 元素文本附加到 [] 中:

<?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 [BUS WEE]</CODAL>
      </RECORD>
      <INTERNALID>5537</INTERNALID>
      <SOMED>1</SOMED>
      <PEMED>1</PEMED>
      <CODAL>PLACEHOLD</CODAL>
   </DATA>
</TABLE>

此外,如果我们可以有一个 txt 文件作为输出,这将有很大帮助,它将包含以下信息:来自文件 a.xml,

INTERNALID: 5511 (and all the rest in a normal xml file) was matched.
INTERNALID: 5510 was matched more than two times, so no join took place.
INTERNALID: 5537 did not match
RECNO 5512 did not have a TOBEEXTRACTED element.
4

2 回答 2

0

这种合并通常可以使用 xsl:for-each-group 来完成:

<xsl:for-each-group select="$doc1//REC, $doc2//REC" group-by="RECNO">
  ...
</xsl:for-each-group>

在正文中, current-group() 使用所需的键保存来自两个文件的记录。例如,您可以将它们分开

<xsl:variable name="doc1rec" select="current-group()[(/) is $doc1]"/>
<xsl:variable name="doc2rec" select="current-group()[(/) is $doc2]"/>

如果您了解逻辑(我不了解),那么剩余的处理应该很简单。

于 2017-05-12T14:41:47.897 回答
0

如果您使用注释中建议的键,则可以引用和匹配元素,如下所示:

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

    <xsl:param name="doc2">
        <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="59">
         <RECNO>5512</RECNO>
      </RECORD>
      <RECORD ID="60">
         <RECNO>5513</RECNO>
         </RECORD>
         <RECORD ID="5511">
            <RECNO>5598</RECNO>
            <TOBEEXTRACTED>FBV</TOBEEXTRACTED>
         </RECORD>

   </DATA>
</TABLE>
    </xsl:param>

    <xsl:key name="ref" match="DATA/RECORD[TOBEEXTRACTED]" use="RECNO"/>

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

    <xsl:template match="DATA/RECORD[key('ref', INTERNALID, $doc2)]/CODAL">
        <xsl:copy>
            <xsl:apply-templates select="node(), key('ref', ../INTERNALID, $doc2)/TOBEEXTRACTED"/>  
        </xsl:copy>
    </xsl:template>

    <xsl:template match="DATA/RECORD[not(key('ref', INTERNALID, $doc2))]"/>

    <xsl:template match="TOBEEXTRACTED">
        <xsl:value-of select="concat(' [', ., ']')"/>
    </xsl:template>

</xsl:transform>

这给出了您在http://xsltransform.net/a9Giwy上发布的输出。在那里我使用了xsl:param name="doc2"带有内联内容的内容,但您当然可以使用它<xsl:param name="doc2" select="doc('fileb.xml')"/>

在编辑中,问题还被标记为我也尝试使用该xsl:merge版本的指令来实现它:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:param name="doc2-uri" as="xs:string" select="'test201705120102.xml'"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:output indent="yes"/>

    <xsl:template match="TABLE/DATA">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:merge>
                <xsl:merge-source name="internal" select="RECORD" >
                    <xsl:merge-key select="INTERNALID"/>
                </xsl:merge-source>
                <xsl:merge-source name="recno" select="doc($doc2-uri)//RECORD">
                    <xsl:merge-key select="RECNO"/>
                </xsl:merge-source>
                <xsl:merge-action>
                    <xsl:if test="current-merge-group('internal') and current-merge-group('recno')">
                        <xsl:copy>
                            <xsl:copy-of select="@*, * except CODAL"/>
                            <CODAL>
                                <xsl:value-of select="CODAL, current-merge-group('recno')/TOBEEXTRACTED/('[' || . || ']')"/>
                            </CODAL>
                        </xsl:copy>
                    </xsl:if>
                </xsl:merge-action>
            </xsl:merge>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
于 2017-05-12T16:28:29.367 回答