XSLT:如何扩展子元素以获得额外的父元素

时间:2012-03-02 19:34:55

标签: xslt transform parent expand

我正在尝试将XML转换为另一个XML文件,但未成功将平面元素更改为扩展元素。

输出应该相同,但DateOfBirth应更改为:

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

2 个答案:

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

应用于提供的(已更正的格式良好的)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

答案 1 :(得分:0)

应该是

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

因为您已经在match=""

中选择了DateOfBirth

在文档结束之前,您在三个结束标记中也缺少/,并且您的命名空间名称无效,因为它们必须是绝对URI。

祝你好运。

相关问题