2012-03-02 70 views
1

我試圖將XML轉換爲另一個XML文件,但未成功將扁平元素更改爲展開的元素。XSLT:如何展開子元素以具有額外的父元素

輸出應該是除了出生日期相同,應改爲:

<DateOfBirth> 
    <FullDate xmlns="cds_dt">1966-02-11</FullDate> 
</DateOfBirth> 

下面是我使用的輸入文件:

Input 
***** 
<?xml version="1.0" encoding="utf-8"?> 
<RootRec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="cds"> 
    <MyRecord> 
    <Demographics> 
     <Names> 
     <LegalName namePurpose="L" xmlns="cds_dt"> 
      <FirstName> 
      <Part>Jason</Part> 
      <PartType>GIV</PartType> 
      </FirstName> 
      <LastName> 
      <Part>Smith</Part> 
      <PartType>FAMC</PartType> 
      </LastName> 
      <OtherName> 
      <Part>Lauren</Part> 
      <PartType>GIV</PartType> 
      </OtherName> 
     </LegalName> 
     </Names> 
     <DateOfBirth>1966-02-11</DateOfBirth> 
    <Demographics> 
    <MyRecord>  
</RootRec> 


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

    <!--Identity Template. This will copy everything as-is.--> 
    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

<!--expand "DateOfBirth" element to /DateOfBirth/FullDate element.--> 
    <xsl:template match="RootRec/MyRecord/Demographics/DateOfBirth"> 
    <DateOfBirth> 
     <FullDate><xsl:value-of select="DateOfBirth"/></FullDate> 
    </DateOfBirth> 
    </xsl:template> 
</xsl:stylesheet> 
+0

你對命名空間的使用很奇怪。 – 2012-03-02 19:43:24

回答

2

這種轉變

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

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

<xsl:template match="x:DateOfBirth/text()"> 
    <xsl:element name="FullDate" xmlns="cds_dt"><xsl:value-of select="."/></xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

適用於pr ovided(校正爲進行簡潔(wellformed))的XML文檔

<RootRec 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns="cds"> 
    <MyRecord> 
     <Demographics> 
      <Names> 
       <LegalName namePurpose="L" xmlns="cds_dt"> 
        <FirstName> 
         <Part>Jason</Part> 
         <PartType>GIV</PartType> 
        </FirstName> 
        <LastName> 
         <Part>Smith</Part> 
         <PartType>FAMC</PartType> 
        </LastName> 
        <OtherName> 
         <Part>Lauren</Part> 
         <PartType>GIV</PartType> 
        </OtherName> 
       </LegalName> 
      </Names> 
      <DateOfBirth>1966-02-11</DateOfBirth> 
     </Demographics> 
    </MyRecord> 
</RootRec> 

產生想要的,正確的結果

<RootRec xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <MyRecord> 
     <Demographics> 
     <Names> 
      <LegalName xmlns="cds_dt" namePurpose="L"> 
       <FirstName> 
        <Part>Jason</Part> 
        <PartType>GIV</PartType> 
       </FirstName> 
       <LastName> 
        <Part>Smith</Part> 
        <PartType>FAMC</PartType> 
       </LastName> 
       <OtherName> 
        <Part>Lauren</Part> 
        <PartType>GIV</PartType> 
       </OtherName> 
      </LegalName> 
     </Names> 
     <DateOfBirth> 
      <FullDate xmlns="cds_dt">1966-02-11</FullDate> 
     </DateOfBirth> 
     </Demographics> 
    </MyRecord> 
</RootRec> 

說明:覆蓋identity rule

+0

謝謝迪米特雷 - 它完美的工作。現在我必須明白你做了什麼...... – user610064 2012-03-02 20:09:28

+0

@ user610064:不客氣。請閱讀我的回答中的鏈接後的*身份規則*。使用和重寫身份模板是最基本和最強大的XSLT設計模式。 – 2012-03-02 20:11:23

0

應該

<FullDate><xsl:value-of select="."/></FullDate> 

,因爲你已經在match=""

選擇出生日期也必須在文件結束前三個結束標記丟失/,和你的名字空間是無效的因爲它們必須是絕對URI。

祝你好運。

相關問題