XSLT:将一个元素(包括子元素)复制到其他节点

时间:2016-04-06 11:28:08

标签: 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>
        <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中的名称标签适合,我想用paymentplan标记替换dataroot标记中的paymentplan标记与父body { margin: 0; padding: 0; width: 100%; } #jumboid { width: 100%; } .bg-image { position: relative; background: url("http://www.visitnorwich.co.uk/assets/Uploads/Events-images/Theatre-generic.jpg") no-repeat center center /cover; height: 450px; background-attachment: fixed; } .bg-image img { display: block; width: 100%; margin: 0 auto; } header { width: 100%; height: 100%; height: calc(100% - 1px); background-image: url('a.jpg'); background-size: cover; }。 。那就是我被困住的地方。任何人都可以帮助我,我是XSLT的新手吗?非常感谢。

2 个答案:

答案 0 :(得分:0)

tf.Example当前copy-of元素或paymentplan相应的copy-of元素。

/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> 替换第二个Laboruntersuchung,这会给出以下输出:

Hauptuntersuchung

答案 1 :(得分:0)

由于您未发布预期的输出XML,因此我很难说出您想要的内容。但是从你的问题来看,我认为你想要改变

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

<site>
    <number>005</number>
    <name>Dr. ABC</name>
    <paymentplan>
        <code>LAB</code>
        <name>Laboruntersuchung</name>
        <service>PAR</service>
    </paymentplan>
    <studycode>STUDYXYZ</studycode>
</site>

这是对的吗?

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

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