2012-03-02 34 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。

祝你好运。

相关问题