Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
Xml XSLT:将一个元素(包括子元素)复制到另一个节点中_Xml_Xslt - Fatal编程技术网

Xml XSLT:将一个元素(包括子元素)复制到另一个节点中

Xml XSLT:将一个元素(包括子元素)复制到另一个节点中,xml,xslt,Xml,Xslt,我搜索了整个网络,但没有找到XML转换问题的解决方案。我有这样一个XML: <?xml version="1.0" encoding="UTF-8"?> <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2016-04-06T10:00:04"> <studyroot> <crocode>AMS</crocode>

我搜索了整个网络,但没有找到XML转换问题的解决方案。我有这样一个XML:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2016-04-06T10:00:04">
    <studyroot>
        <crocode>AMS</crocode>
        <croname>AMS GmbH</croname>
        <exportdatetime>2016-04-04T10:17:59</exportdatetime>
        <studynumer>STUDYXYZ</studynumer>
        <site>
            <number>005</number>
            <name>Dr. ABC</name>
            <paymentplan>Laboruntersuchung</paymentplan>
            <studycode>STUDYXYZ</studycode>
        </site>
        <site>
            <number>016</number>
            <name>Dr. XYZ</name>
            <paymentplan>Laboruntersuchung</paymentplan>
            <studycode>STUDYXYZ</studycode>
        </site>
        <site>
            <number>053</number>
            <name>Dr. DEF</name>
            <patient>01</patient>
            <paymentplan>Laboruntersuchung</paymentplan>
            <studycode>STUDYXYZ</studycode>
        </site>
    </studyroot>
    <patient>
        <site>053</site>
        <number>01</number>
        <service>Hauptuntersuchung</service>
    </patient>
    <service>
        <site>053</site>
        <pat>01</pat>
        <code>HAU</code>
        <iteration>1</iteration>
        <name>Hauptuntersuchung</name>
        <done>0</done>
        <obsolete>0</obsolete>
        <completedate>2016-04-04T00:00:00</completedate>
    </service>
    <service>
        <site>053</site>
        <code>PAR</code>
        <iteration>1</iteration>
        <name>Laboruntersuchung</name>
        <done>0</done>
        <obsolete>0</obsolete>
        <completedate>2016-04-04T00:00:00</completedate>
    </service>
    <paymentplan>
        <code>LAB</code>
        <name>Laboruntersuchung</name>
        <service>PAR</service>
    </paymentplan>
    <service>
        <site>053</site>
        <pat>01</pat>
        <code>HAU</code>
        <iteration>1</iteration>
        <name>Hauptuntersuchung</name>
        <done>0</done>
        <obsolete>0</obsolete>
        <completedate>2016-04-04T00:00:00</completedate>
    </service>
    <service>
        <site>053</site>
        <code>PAR</code>
        <iteration>1</iteration>
        <name>Laboruntersuchung</name>
        <done>0</done>
        <obsolete>0</obsolete>
        <completedate>2016-04-04T00:00:00</completedate>
    </service>
</dataroot>

作为第一步,如果
paymentplan
中的名称标记合适,我想用带有父级
dataroot
paymentplan
标记替换每个站点标记中的
paymentplan
标记。这就是我被困的地方。有人能帮我吗,我对XSLT很陌生?非常感谢。

当前
付款计划
元素或相应
/dataroot/paymentplan
元素的
副本

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

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

<xsl:template match="paymentplan[local-name(..) = 'site']">
  <xsl:variable name="curUntersuchung" select="text()" />
  <xsl:choose>
    <xsl:when test="/dataroot/paymentplan[name/text() = $curUntersuchung]">
      <xsl:copy-of select="/dataroot/paymentplan[name/text() = $curUntersuchung]" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of select="." />
    </xsl:otherwise>
  </xsl:choose> 
</xsl:template>   

</xsl:stylesheet>

我很难告诉您想要什么,因为您没有发布预期的输出XML。但从你的问题来看,我认为你想要转变

<site>
    <number>005</number>
    <name>Dr. ABC</name>
    <paymentplan>Laboruntersuchung</paymentplan>
    <studycode>STUDYXYZ</studycode>
</site>
对吗

如果是这样,那么您的XSL模板将是:

<xsl:template match="//site/paymentplan">
    <xsl:apply-templates select="ancestor::dataroot/paymentplan[name=current()]"/>
</xsl:template>

在这种情况下,您能显示预期的输出吗?非常感谢。
<site>
    <number>005</number>
    <name>Dr. ABC</name>
    <paymentplan>
        <code>LAB</code>
        <name>Laboruntersuchung</name>
        <service>PAR</service>
    </paymentplan>
    <studycode>STUDYXYZ</studycode>
</site>
<xsl:template match="//site/paymentplan">
    <xsl:apply-templates select="ancestor::dataroot/paymentplan[name=current()]"/>
</xsl:template>