Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果某个元素匹配,XSLT将该元素从一个xml复制到另一个xml_Xslt_Saxon - Fatal编程技术网

如果某个元素匹配,XSLT将该元素从一个xml复制到另一个xml

如果某个元素匹配,XSLT将该元素从一个xml复制到另一个xml,xslt,saxon,Xslt,Saxon,我有以下xml: 通知-source-path.xml <?xml version="1.0" encoding="UTF-8"?> <Notifications> <Notification> <NotifId>1</NotifId> <MsgText> <![CDATA[notif 1]]> </MsgText>

我有以下xml:

通知-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>

1.
2.
和通知.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>

1123213333
1.
abc@def.ghi
SBJ2
2.
jkl。mno@pqr
SBJ2
如果
匹配,我需要将
从notifications-source-path.xml复制到notifications.xml(notifications/Notification/EmailNotification/MsgText在标记主题之后)。有人能告诉我如何正确实施这一点吗?我打算用撒克逊河图书馆来做这个

编辑:

因此,到目前为止,我已经创建了以下代码:

<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_通知中选择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>

1123213333
1.
abc@def.ghi
SBJ2
2017-12-31T10:00:99
2.
jkl。mno@pqr
SBJ2
2017-12-31T10:00:99
但我不知道如何比较呈报人,然后应用它 将选定的MsgText转换为目标xml

您使用的是Saxon HE,最新版本的Saxon HE支持XSLT 3.0,XSLT 3.0有一个新的指令xsl:merge,它是为这个需求定制的。您需要这样的内容(修改以考虑有关所需结果的新信息):


1123213333
1.
abc@def.ghi
SBJ2
2.
jkl。mno@pqr
SBJ2
1.
2.
{(current-merge-group()/NotifId)[1]}
这提供了预期的结果,除了(a)TransferTime元素丢失(我看不出从何处获得),以及(b)XSLT无法将CDATA节从输入复制到输出(这里的CDATA没有任何有用的用途)

如果您想要一个XSLT2.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>

1123213333
1.
abc@def.ghi
SBJ2
2.
jkl。mno@pqr
SBJ2
1.
2.

欢迎来到StackOverflow。请访问并阅读。StackOverflow的工作效果最好,如果你发布你迄今为止所做的事情。像“请为我写代码”这样的问题(即使你不是有意这样做)通常会被否决,并作为离题而搁置。如果您展示了您尝试过的内容,并解释了哪些不起作用(包括堆栈跟踪和/或错误消息,如果合适的话),您就更有可能获得帮助。请阅读Hello,不幸的是,这只复制元素的值,而不是元素标记。我使用的是saxon-saxon-HE-9.6.0-6.jar som,如果还有XLST3.0,我就不是usre。我是xslt的初学者。您还没有显示所需的输出,所以我不得不猜测。如果您想要XSLT 3.0支持,请切换到Saxon HE 9.8。我已经用target更新了这个问题。我想切换,如果没有其他选择,我会这样做,但我使用的是一些已经包含此库的产品。如果版本中没有其他选项,我将创建新的jar,并以某种方式执行,但为了简单起见,我希望使用当前使用的版本。好的,我认为与我的猜测的主要区别是,您希望MsgText位于EmailNotification内部,而不是外部。我将编辑示例以实现这一点。请注意,您不能通过XSLT转换来保留CDATA部分,这似乎是您所希望的。CDATA是否会通过
保留?
<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>