【问题标题】:XSLT copy element from one xml to other xml if some element matches如果某些元素匹配,XSLT 将元素从一个 xml 复制到另一个 xml
【发布时间】:2017-06-26 15:02:06
【问题描述】:

我有以下 xml:

notifications-source-path.xml

<?xml version="1.0" encoding="UTF-8"?>
<Notifications>
    <Notification>
        <NotifId>1</NotifId>
        <MsgText>
            <![CDATA[notif 1]]>
        </MsgText>
    </Notification>
    <Notification>
        <NotifId>2</NotifId>
        <MsgText>
            <![CDATA[notif 2]]>
        </MsgText>
    </Notification>
</Notifications>

和通知.xml

<?xml version="1.0" encoding="UTF-8"?>
<Notifications>
    <BatchId>1123213333</BatchId>
    <Notification>
        <NotifId>1</NotifId>
        <EmailNotification>
            <SenderAddress>abc@def.ghi</SenderAddress>
            <Subject>SBJ2</Subject>
        </EmailNotification>
    </Notification>
    <Notification>
        <NotifId>2</NotifId>
        <EmailNotification>
            <SenderAddress>jkl.mno@pqr</SenderAddress>
            <Subject>SBJ2</Subject>
        </EmailNotification>
    </Notification>
</Notifications>

如果&lt;NotifId&gt; 匹配,我需要将&lt;MsgText&gt; 从notifications-source-path.xml 复制到notifications.xml(标签主题后的Notifications/Notification/EmailNotification/MsgText)。有人可以告诉我如何正确实现这一点吗?我打算为此使用 saxon-he 库。

编辑:

所以到目前为止我已经创建了这个代码:

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


    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"
                cdata-section-elements="MsgText"/>

    <xsl:param name="notifications-source-path" select="'html_notifications.xml'"/>

    <xsl:template match="Notifications/Notification">

        <xsl:apply-templates select="NotifId"/>

    </xsl:template>

    <xsl:template match="NotifId">
        <xsl:variable name="current.notifId" select="NotifId/text()"/>

        <MsgText>
            <xsl:copy-of
                    select="document($notifications-source-path)/Notifications/Notification/NotifId/../MsgText/node()"/>
        </MsgText>
    </xsl:template>

</xsl:stylesheet>

它从 html_notifications 中选择了 MsgText。但我不知道如何比较 NotifId,然后将选定的 MsgText 应用于目标 xml。

编辑2: 输出应该是:

<?xml version="1.0" encoding="UTF-8"?>
<Notifications>
  <BatchId>1123213333</BatchId>
  <Notification>
    <NotifId>1</NotifId>
    <EmailNotification>
      <SenderAddress>abc@def.ghi</SenderAddress>
      <Subject>SBJ2</Subject>
      <MsgText><![CDATA[notif 1]]></MsgText>
      <TransferTime>2017-12-31T10:00:99</TransferTime>
    </EmailNotification>
  </Notification>
  <Notification>
    <NotifId>2</NotifId>
    <EmailNotification>
      <SenderAddress>jkl.mno@pqr</SenderAddress>
      <Subject>SBJ2</Subject>
      <MsgText><![CDATA[notif 2]]></MsgText>
      <TransferTime>2017-12-31T10:00:99</TransferTime>
    </EmailNotification>
  </Notification>
</Notifications>

【问题讨论】:

  • 欢迎来到 StackOverflow。请访问help center 并阅读How to Ask。如果您发布到目前为止所做的工作,StackOverflow 效果最好。看起来像“请为我写代码”的问题(即使你不是这样打算的)通常会被否决并被搁置为题外话。如果您展示了您尝试过的方法并解释了哪些方法不起作用(包括堆栈跟踪和/或适当的错误消息),那么您更有可能获得帮助。请阅读Why is “Can someone help me?” not an actual question?

标签: xslt saxon


【解决方案1】:

但我不知道如何比较 NotifId 然后应用它 选择 MsgText 来定位 xml。

您正在使用 Saxon-HE,并且最新版本的 Saxon-HE 支持 XSLT 3.0,并且 XSLT 3.0 有一个新指令 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" expand-text="yes">

    <!--<xsl:variable name="notifications" select="doc('notifications.xml')"/>
        <xsl:variable name="notifications-source-path" select="doc('notifications-source-path.xml')"/>-->

    <xsl:variable name="notifications">
        <Notifications>
            <BatchId>1123213333</BatchId>
            <Notification>
                <NotifId>1</NotifId>
                <EmailNotification>
                    <SenderAddress>abc@def.ghi</SenderAddress>
                    <Subject>SBJ2</Subject>
                </EmailNotification>
            </Notification>
            <Notification>
                <NotifId>2</NotifId>
                <EmailNotification>
                    <SenderAddress>jkl.mno@pqr</SenderAddress>
                    <Subject>SBJ2</Subject>
                </EmailNotification>
            </Notification>
        </Notifications>
    </xsl:variable>

    <xsl:variable name="notifications-source-path">
        <Notifications>
            <Notification>
                <NotifId>1</NotifId>
                <MsgText>
                    <![CDATA[notif 1]]>
                </MsgText>
            </Notification>
            <Notification>
                <NotifId>2</NotifId>
                <MsgText>
                    <![CDATA[notif 2]]>
                </MsgText>
            </Notification>
        </Notifications>
    </xsl:variable>

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

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>


    <xsl:template name="xsl:initial-template">
        <Notifications>
            <xsl:copy-of select="$notifications//BatchId"/>
            <xsl:merge>
                <xsl:merge-source name="notifications" select="$notifications/*/Notification">
                    <xsl:merge-key select="NotifId"/>
                </xsl:merge-source>
                <xsl:merge-source name="notifications-source-path"
                    select="$notifications-source-path/*/Notification">
                    <xsl:merge-key select="NotifId"/>
                </xsl:merge-source>
                <xsl:merge-action>
                    <Notification>
                        <NotifId>{(current-merge-group()/NotifId)[1]}</NotifId>
                        <EmailNotification>
                            <xsl:apply-templates select="current-merge-group()/EmailNotification/*, current-merge-group()/MsgText"/>
                        </EmailNotification>
                    </Notification>
                </xsl:merge-action>
            </xsl:merge>
        </Notifications>
    </xsl:template>


</xsl:stylesheet>

这提供了预期的结果,除了 (a) 缺少 TransferTime 元素 - 我看不出你从哪里得到它,以及 (b) XSLT 无法将 CDATA 部分从输入复制到输出 - 这里的 CDATA无论如何都没有任何用处。

如果您需要 XSLT 2.0 解决方案,您可以使用 xsl:for-each-group 获得非常相似的结果。这是这个版本:

<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="2.0">

    <!--<xsl:variable name="notifications" select="doc('notifications.xml')"/>
        <xsl:variable name="notifications-source-path" select="doc('notifications-source-path.xml')"/>-->

    <xsl:variable name="notifications">
        <Notifications>
            <BatchId>1123213333</BatchId>
            <Notification>
                <NotifId>1</NotifId>
                <EmailNotification>
                    <SenderAddress>abc@def.ghi</SenderAddress>
                    <Subject>SBJ2</Subject>
                </EmailNotification>
            </Notification>
            <Notification>
                <NotifId>2</NotifId>
                <EmailNotification>
                    <SenderAddress>jkl.mno@pqr</SenderAddress>
                    <Subject>SBJ2</Subject>
                </EmailNotification>
            </Notification>
        </Notifications>
    </xsl:variable>

    <xsl:variable name="notifications-source-path">
        <Notifications>
            <Notification>
                <NotifId>1</NotifId>
                <MsgText>
                    <![CDATA[notif 1]]>
                </MsgText>
            </Notification>
            <Notification>
                <NotifId>2</NotifId>
                <MsgText>
                    <![CDATA[notif 2]]>
                </MsgText>
            </Notification>
        </Notifications>
    </xsl:variable>

    <xsl:template match="*">
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>


    <xsl:template name="main">
        <Notifications>
            <xsl:copy-of select="$notifications//BatchId"/>
            <xsl:for-each-group select="($notifications, $notifications-source-path)/*/Notification" group-by="NotifId">
                    <Notification>
                        <NotifId><xsl:value-of select="current-grouping-key()"/></NotifId>
                        <EmailNotification>
                            <xsl:apply-templates select="current-group()/EmailNotification/*, current-group()/MsgText"/>
                        </EmailNotification>
                    </Notification>
            </xsl:for-each-group>
        </Notifications>
    </xsl:template>


</xsl:stylesheet>

【讨论】:

  • 您好,不幸的是,这只会复制元素的值而不是元素标签。而且我正在使用 saxon Saxon-HE-9.6.0-6.jar som,如果还有 XLST 3.0,我就不知道了。我是 xslt 的初学者。
  • 你没有显示你想要的输出,所以我不得不猜测。如果需要 XSLT 3.0 支持,请切换到 Saxon-HE 9.8。
  • 我已经用目标更新了问题。如果没有其他选择,我想切换并且会这样做,但是我正在使用一些已经包含该库的产品。如果没有其他版本的选项,我将创建新的 jar,它将以某种方式执行,但为了简单起见,我想坚持使用当前使用的版本。
  • 好的,我认为与我猜测的主要区别是您希望 MsgText 在 EmailNotification 内,而不是在它之外。我将编辑示例以实现这一目标。请注意,您无法通过 XSLT 转换保留 CDATA 部分,如您所愿。
  • 不会用 &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" cdata-section-elements="MsgText"/&gt; 保留 CDATA?
猜你喜欢
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
相关资源
最近更新 更多