2012-03-02 24 views
1

XMLを別のXMLファイルに変換しようとしましたが、フラット要素を展開要素に変更できませんでした。XSLT:子要素に親要素を追加する方法

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

あなたの名前空間の使用は非常に奇妙です。 –

答えて

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> 
ここ

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

は、私が使用していた入力ファイルがあります

ovided(WELLFORMED行われるように修正)XMLドキュメント

<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をオーバーライド

<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> 

指名手配、正しい結果を生成します。

+0

Dimitreに感謝 - それは完璧に働いた。今あなたがしたことを理解しなければなりません。 – user610064

+0

@ user610064:どうぞよろしくお願いします。私の答えにあるリンクの後にある*アイデンティティルール*についてお読みください。アイデンティティテンプレートの使用と上書きは、最も基本的で最も強力なXSLTデザインパターンです。 –

0

あなたはすでにあなたはまた、文書の終了前に3個の終了タグで/が欠落している、とあなたの名前空間名が無効であるmatch=""

でdateOfBirthのを選択しているので、それは

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

する必要があります絶対URIでなければならないからです。

幸運。

関連する問題