gpt4 book ai didi

xml - 在子节点中使用应用模板和字符串操作的新手转换

转载 作者:行者123 更新时间:2023-12-04 06:22:22 25 4
gpt4 key购买 nike

我有一个 xml 文件,我想转换成一个 xml 文件。

输入 XML 文件:

<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
<CONTACT>
<FirstName>Arun_niit</FirstName>
<LastName>Arun_niit</LastName>
<EMail>nura_ice@yahoo.co.in</EMail>
</CONTACT>
<CONTACT>
<FirstName>FodenBen'</FirstName>
<LastName>Ben' Foden</LastName>
<URL>http://www.facebook.com/profile.php?id=100002440474277</URL>
</CONTACT>
<CONTACT>
<FirstName>GhorbelMahmoud</FirstName>
<LastName>Mahmoud Ghorbel</LastName>
<EMail>mahmoud.ghorbel@alcatel-lucent.com</EMail>
</CONTACT>
<CONTACT>
<FirstName>keyankarthik</FirstName>
<LastName>karthik keyan</LastName>
<EMail>karthycse@yahoo.co.in</EMail>
</CONTACT>
<CONTACT>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<EMail>kart2006@gmail.com</EMail>
<EMail>karthikrangaraj@yahoo.com</EMail>
</CONTACT>
<CONTACT>
<FirstName>ReddyAkky</FirstName>
<LastName>Akky Reddy</LastName>
<EMail>akkireddych@gmail.com</EMail>
</CONTACT>
<CONTACT>
<FirstName>SandfordFrankie</FirstName>
<LastName>Frankie Sandford</LastName>
<URL>http://www.facebook.com/FrankieSandfordApprovedPage</URL>
</CONTACT>
<CONTACT>
<FirstName>TheSatsRochelle</FirstName>
<LastName>Rochelle TheSats</LastName>
<URL>http://www.facebook.com/profile.php?id=100002487211054</URL>
</CONTACT>
<CONTACT>
<FirstName>KumarVeera</FirstName>
<LastName>Veera_Kumar</LastName>
<EMail>KUMARg_81@yahoo.com</EMail>
</CONTACT>
</CONTACTS>

我想要这样的输出:
        <?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT>
<PDE-Identity>N65539</PDE-Identity>
<FirstName>Arun_niit</FirstName>
<LastName>Arun_niit</LastName>
<Facebook-ID/>
<Emails>
<Email Type="yahoo" Value="nura_ice@yahoo.co.in"/>
</Emails>
</CONTACT>

<CONTACT>
<PDE-Identity>N65546</PDE-Identity>
<FirstName>FodenBen'</FirstName>
<LastName>Ben' Foden</LastName>
<URL>http://www.facebook.com/profile.php?id=100002440474277</URL>
<Facebook-ID>100002440474277</Facebook-ID>
<EMAILS/>
</CONTACT>

<CONTACT>
<PDE-Identity>N65553</PDE-Identity>
<FirstName>GhorbelMahmoud</FirstName>
<LastName>Mahmoud Ghorbel</LastName>
<Facebook-ID/>
<Emails>
<Email Type="alcatel-lucent" Value="mahmoud.ghorbel@alcatel-lucent.com"/>
</Emails>

<CONTACT>
<PDE-Identity>N65567</PDE-Identity>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<Facebook-ID/>
<Emails>
<Email Type="gmail" Value="kart2006@gmail.com"/>
<Email Type="yahoo" Value="karthikrangaraj@yahoo.com"/>
</Emails>
</CONTACT>

</CONTACTS>

任何想法家伙,我已经了解了 xslt,我尝试添加如下:

这是我的 xslt 文件:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="CONTACT">
<xsl:copy>
<PDE-Identity>
<xsl:value-of select="generate-id(.)"/>
</PDE-Identity>
<xsl:copy-of select="FirstName|LastName|URL"/>
<Facebook-ID><!-- To extract the ID value from URL/Email -->
<xsl:choose>
<xsl:when test="URL">
<xsl:value-of select="substring-after(URL,'?id=')"/>
</xsl:when>
<xsl:otherwise>
<!-- <xsl:value-of select="substring-before(EMail[1],'@')"/>-->
</xsl:otherwise>
</xsl:choose>
</Facebook-ID>
<EMAILS>
<xsl:apply-templates select="EMail"/>
</EMAILS>
</xsl:copy>
</xsl:template>

<xsl:template match="EMail">
<EMail Type="<xsl:value-of select="substring-before(substring-after(.,'@'), '.')"/>" Value="<xsl:value-of select="."/>"/>
</xsl:template>

</xsl:stylesheet>

它没有用。请帮帮我吧伙计们。示例代码;谢谢你们。

最佳答案

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

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

<xsl:template match="CONTACT">
<xsl:copy>
<xsl:copy-of select="FirstName|LastName"/>
<EMAILS>
<xsl:apply-templates select="EMail"/>
</EMAILS>
</xsl:copy>
</xsl:template>

<xsl:template match="EMail">
<EMail>
<type><xsl:value-of select="substring-before(
substring-after(.,'@'),
'.')"/>
</type>
<value><xsl:value-of select="."/></value>
</EMail>
</xsl:template>

</xsl:stylesheet>

根据您的输入,产生:
<?xml version="1.0" encoding="utf-8"?>
<CONTACTS>
<CONTACT>
<FirstName>AfgZohal</FirstName>
<LastName>Zohal Afg</LastName>
<EMAILS/>
</CONTACT>
<CONTACT>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<EMAILS>
<EMail>
<type>gmail</type>
<value>kart2006@gmail.com</value>
</EMail>
<EMail>
<type>yahoo</type>
<value>karthikrangaraj@yahoo.com</value>
</EMail>
</EMAILS>
</CONTACT>
</CONTACTS>

关于xml - 在子节点中使用应用模板和字符串操作的新手转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6394489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com