xslt 将元素从 header 节点添加到另一个节点(不是 header 的 children)

xlst to add elements from header node into other nodes (not children of header)

这是问题的扩展:

我正在尝试将 2 个元素 PeriodStartDate 和 PeriodEndDate 插入此 xml 文件的 Cuesheet 节点中:

<?xml version="1.0" encoding="utf-8"?>
<CueSheets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    
<Header>
    <ReportType>C</ReportType>
    <ReportPeriodStartDate>20110101</ReportPeriodStartDate>
    <ReportPeriodEndDate>20150814</ReportPeriodEndDate>
</Header>
<CueSheet>
    <NewOrUpdate>N</NewOrUpdate>
    <EbiquityId>7234709</EbiquityId>
    <EbiquityFilename>7234709_1.mpg</EbiquityFilename>
    <AdTitle>2015- Available Now At The Warehouse.</AdTitle>
    <AdDescription>Artists listed. Retailers listed.</AdDescription>
    <AdDuration>00:00:15</AdDuration>
    <FirstTransmissionDate>20150212</FirstTransmissionDate>
    <FirstTransmissionStation>FOUR</FirstTransmissionStation>
    <Brand>Summer Mix Tape</Brand>
    <Product>cd release</Product>
        <Cue>
            <TrackSequenceNumber>3</TrackSequenceNumber>
            <TrackTitle>Geronimo</TrackTitle>
            <Artists>
            <Artist>Sheppard</Artist>
            </Artists>
            <Composers>
                <Composer>George Sheppard</Composer>
                <Composer>Amy Sheppard ,Jay Bovino</Composer>
            </Composers>
            <ProductionMusic>N</ProductionMusic>
            <RecordLabels>
                <RecordLabel>UMI Decca Records</RecordLabel>
            </RecordLabels>
            <ISRCs>
                <ISRC>AU-IYA-14-00002</ISRC>
            </ISRCs>
            <ARID>204313468</ARID>
            <TimeIn>00:00:09</TimeIn>
            <TimeOut>00:00:15</TimeOut>
            <Duration>00:00:06</Duration>
        </Cue>
    <Complete>Y</Complete>
</CueSheet>
</CueSheets>

这是 xslt 的原样(正在将 Cuesheets 中的 EbiquityId 插入到 Cue 节点中,所以我知道我在正确的轨道上感谢@michael.kor257 和@Parfait):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="periodStart" select="/Header/ReportPeriodStartDate"/>
<xsl:variable name="periodEnd" select="/Header/ReportPeriodEndDate"/>

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

<xsl:template match="Cuesheet">
    <xsl:copy>    
        <ReportPeriodStartDate>
            <xsl:value-of select="$periodStart"/>
        </ReportPeriodStartDate>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Cuesheet">
    <xsl:copy>    
        <ReportPeriodEndDate>
            <xsl:value-of select="$periodEnd"/>
        </ReportPeriodEndDate>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Cue|Artists|Composers|RecordLabels|ISRCs">
    <xsl:copy>    
        <EbiquityId>
            <xsl:value-of select="ancestor::CueSheet/EbiquityId"/>
        </EbiquityId>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

在此先感谢您提供的任何帮助。

目前您有匹配 Cuesheet 的模板,这是不允许的。当尝试匹配节点时,XSLT 处理器要么发出错误信号,要么只选择最后一个模板。另请注意,XSLT 区分大小写。模板匹配 Cuesheet,但 XML 中的元素是 CueSheet

此外,periodStartperiodEnd 的定义不正确,因为它正在寻找 Header 的根元素,但您的根元素是 CueSheets,所以它应该是这样的

<xsl:variable name="periodStart" select="/CueSheets /Header/ReportPeriodStartDate"/>

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="periodStart" select="/CueSheets/Header/ReportPeriodStartDate"/>
<xsl:variable name="periodEnd" select="/CueSheets/Header/ReportPeriodEndDate"/>

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

<xsl:template match="CueSheet">
    <xsl:copy>    
        <ReportPeriodStartDate>
            <xsl:value-of select="$periodStart"/>
        </ReportPeriodStartDate>
        <ReportPeriodEndDate>
            <xsl:value-of select="$periodEnd"/>
        </ReportPeriodEndDate>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Cue|Artists|Composers|RecordLabels|ISRCs">
    <xsl:copy>    
        <EbiquityId>
            <xsl:value-of select="ancestor::CueSheet/EbiquityId"/>
        </EbiquityId>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

这是解决这个问题的可能性。然后将 Report-Node-Names 放入变量中:

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

 <xsl:template match="@*|node()">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()[local-name()!='Header']"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="CueSheet">
  <xsl:element name="CueSheet">
   <xsl:copy-of select="node()" />
   <xsl:copy-of select="../Header/ReportPeriodStartDate" />
   <xsl:copy-of select="../Header/ReportPeriodEndDate" />
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>