xlst将标题节点中的元素添加到其他节点(不是标题的子节点)

时间:2015-12-11 08:47:41

标签: xml xslt transformation

这是问题的扩展:

How to import XML with layers of nested nodes (parent/child/child relationships) into Access?

我正在尝试将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>

提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

目前,您有匹配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>

答案 1 :(得分:0)

这是解决这个问题的可能性。然后将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>
相关问题