0

我想用另一个 XML 文件的等效节点替换 XML 文件的某些节点。由于这不够具有挑战性,我希望用于比较的 ID 是某个孩子的值。

“旧” XML 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Documents>
        <Document id="001">
            <Tags>
                <Tag id="document_id">someIDfilename.pdf</Tag>
                <Tag id="document_type">Type A</Tag>
                <Tag id="document_text">A very important document of course.</Tag>
            <Tags>
        </Document>
        <Document id="018">
            <Tags>
                <Tag id="document_id">someOtherIDfilename.pdf</Tag>
                <Tag id="document_type">Type B</Tag>
                <Tag id="document_text">Another very important document.</Tag>
            <Tags>
        </Document>
    </Documents>
</Root>

第二个文档应替换为以下 XML 的等效项,其中我必须使用的 ID 是 document_id 的值(因为有时会覆盖或更改 Document 节点的“id”):

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Documents>
        <Document id="014">
            <Tags>
                <Tag id="document_id">someOtherIDfilename.pdf</Tag>
                <Tag id="document_type">Type B</Tag>
                <Tag id="document_text">The oh so important new document text.</Tag>
            <Tags>
        </Document>
    </Documents>
</Root>

结果预计如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Documents>
        <Document id="001">
            <Tags>
                <Tag id="document_id">someIDfilename.pdf</Tag>
                <Tag id="document_type">Type A</Tag>
                <Tag id="document_text">A very important document of course.</Tag>
            <Tags>
        </Document>
        <Document id="018">
            <Tags>
                <Tag id="document_id">someOtherIDfilename.pdf</Tag>
                <Tag id="document_type">Type B</Tag>
                <Tag id="document_text">The oh so important new document text.</Tag>
            <Tags>
        </Document>
    </Documents>
</Root>

Q1:这可以通过 XSLT 实现吗?还是我必须使用 Java / DOM?

Q2:如果 Q1==yes :有人可以在这里解决吗?

最好的!菲利普

4

1 回答 1

3

使用像 Saxon 9 这样的 XSLT 2.0 处理器:

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

<xsl:param name="doc2-url" select="'test2014032603.xml'"/>
<xsl:variable name="doc2" select="doc($doc2-url)"/>

<xsl:key name="id" match="Document" use="Tags/Tag[@id = 'document_id']"/>

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

<xsl:template match="Document[key('id', Tags/Tag[@id = 'document_id'], $doc2)]">
  <xsl:copy>
    <xsl:copy-of select="@id, key('id', Tags/Tag[@id = 'document_id'], $doc2)/node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

使用 XSLT 1.0 也是可能的,但要在文档之间切换上下文以使用密钥,代码最终会变得复杂:

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

<xsl:param name="doc2-url" select="'test2014032603.xml'"/>
<xsl:variable name="doc2" select="document($doc2-url)"/>

<xsl:key name="id" match="Document" use="Tags/Tag[@id = 'document_id']"/>

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

<xsl:template match="Document[Tags/Tag[@id = 'document_id']]">
  <xsl:variable name="this" select="."/>
  <xsl:for-each select="$doc2">
    <xsl:choose>
      <xsl:when test="key('id', $this/Tags/Tag[@id = 'document_id'])">
        <xsl:for-each select="key('id', $this/Tags/Tag[@id = 'document_id'])">
          <xsl:copy>
            <xsl:copy-of select="$this/@id"/>
            <xsl:copy-of select="node()"/>            
          </xsl:copy>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$this"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
于 2014-03-26T17:22:07.937 回答