我正在尝试转换以下XML
我有一个转换,我需要添加一个额外的部分。
这是XML文件的当前格式。
<?xml version="1.0" encoding="utf-8" ?>
<contacts>
<contact>
<attribute>
<name>text12</name>
<value>B00085590</value>
</attribute>
<attribute>
<name>text34</name>
<value>Atomos</value>
</attribute>
<attribute>
<name>date866</name>
<value>02/21/1991</value>
</attribute>
<attribute>
<name>text123</name>
<value>brady@gmail.com</value>
</attribute>
<attribute>
<name>text875</name>
<value>123-456-7890</value>
</attribute>
</contact>
<contact>
<attribute>
<name>text12</name>
<value>B00058478</value>
</attribute>
<attribute>
<name>text34</name>
<value>Balderas</value>
</attribute>
<attribute>
<name>date866</name>
<value>11/24/1997</value>
</attribute>
<attribute>
<name>text123</name>
<value>balderas@yahoo.com</value>
</attribute>
<attribute>
<name>text875</name>
<value>098-765-4321</value>
</attribute>
</contact>
</contacts>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<!--Identify location of the lookup xml-->
<xsl:param name="lookupDoc" select="document('C:\Projects\FormattedAttributes.xml')" />
<!--Lookup Key-->
<xsl:key name="att-lookup" match="attributes/attribute" use="name"/>
<!--Main Template-->
<xsl:template match="/contacts">
<contacts>
<!--Apply Formatted Contacts Template-->
<xsl:apply-templates select="contact" />
</contacts>
</xsl:template>
<!--Formatted Contacts Template-->
<xsl:template match="contact">
<contact>
<xsl:for-each select="attribute">
<!--Create variable to hold New Name after passing the Data Name to the Lookup Key-->
<xsl:variable name="name" select="name"/>
<xsl:variable name="newName">
<xsl:for-each select="$lookupDoc">
<xsl:apply-templates select="key('att-lookup', $name)/mappingname"/>
</xsl:for-each>
</xsl:variable>
<!--Format Contact Element with New Name variable-->
<xsl:element name="{$newName}">
<xsl:value-of select="value"/>
</xsl:element>
</xsl:for-each>
</contact>
</xsl:template>
</xsl:stylesheet>
这是转换中使用的查找XML的示例
<?xml version="1.0" encoding="utf-8" ?>
<attributes>
<attribute folder="PERSONAL">
<name>text12</name>
<mappingname>ID</mappingname>
<datatype>Varchar2</datatype>
<size>30</size>
</attribute>
<attribute folder="PERSONAL">
<name>text34</name>
<mappingname>LAST_NAME</mappingname>
<datatype>Varchar2</datatype>
<size>30</size>
</attribute>
<attribute folder="PERSONAL">
<name>date866</name>
<mappingname>DOB</mappingname>
<datatype>Date</datatype>
<size></size>
</attribute>
<attribute folder="CONTACT_INFO">
<name>text123</name>
<mappingname>EMAIL</mappingname>
<datatype>Varchar2</datatype>
<size>30</size>
</attribute>
<attribute folder="CONTACT_INFO">
<name>text875</name>
<mappingname>PHONE</mappingname>
<datatype>Varchar2</datatype>
<size>20</size>
</attribute>
</attributes>
目前,我的转换像一个符咒一样提供了以下输出
<?xml version="1.0" encoding="utf-8" ?>
<contacts>
<contact>
<ID>B00085590</ID>
<LAST_NAME>Brady</LAST_NAME>
<DOB>02/21/1991</DOB>
<EMAIL>brady@gmail.com</EMAIL>
<PHONE>123-456-7890</PHONE>
</contact>
<contact>
<ID>B00058478</ID>
<LAST_NAME>Balderas</LAST_NAME>
<DOB>11/24/1997</DOB>
<EMAIL>balderas@yahoo.com</EMAIL>
<PHONE>098-765-4321</PHONE>
</contact>
</contacts>
我的需求已经更改,我需要更改最后的转换,以考虑文件夹属性,并将ID元素作为所有新创建的文件夹父属性的属性。
<?xml version="1.0" encoding="utf-8" ?>
<data>
<PERSONAL ID="B00085590">
<LAST_NAME>Brady</LAST_NAME>
<DOB>02/21/1991</DOB>
</PERSONAL>
<PERSONAL ID="B00058478">
<LAST_NAME>Balderas</LAST_NAME>
<DOB>11/24/1997</DOB>
</PERSONAL>
<CONTACT_INFO ID="B00085590">
<EMAIL>brady@gmail.com</EMAIL>
<PHONE>123-456-7890</PHONE>
</CONTACT_INFO>
<CONTACT_INFO ID="B00058478">
<EMAIL>balderas@yahoo.com</EMAIL>
<PHONE>098-765-4321</PHONE>
</CONTACT_INFO>
</data>