【问题标题】:XSLT: Copy one element (including children) into other nodeXSLT:将一个元素(包括子元素)复制到另一个节点
【发布时间】:2016-04-06 11:28:08
【问题描述】:

我已经搜索了整个网络,但我没有找到解决我的 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 中的名称标签适合,我想用父datarootpaymentplan 标签替换每个站点标签中的paymentplan 标签。这就是我卡住的地方。谁能帮助我,我对 XSLT 很陌生?提前非常感谢。

【问题讨论】:

  • 你能在这种情况下显示你期望的输出吗?谢谢!

标签: xml xslt


【解决方案1】:

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,输出如下:

<?xml version="1.0"?>
<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>
                <code>LAB</code>
                <name>Laboruntersuchung</name>
                <service>PAR</service>
            </paymentplan>
            <studycode>STUDYXYZ</studycode>
        </site>
        <site>
            <number>016</number>
            <name>Dr. XYZ</name>
            <paymentplan>Hauptuntersuchung</paymentplan>
            <studycode>STUDYXYZ</studycode>
        </site>
        <site>
            <number>053</number>
            <name>Dr. DEF</name>
            <patient>01</patient>
            <paymentplan>
                <code>LAB</code>
                <name>Laboruntersuchung</name>
                <service>PAR</service>
            </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>

【讨论】:

    【解决方案2】:

    我很难说出您想要什么,因为您没有发布预期的输出 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多