使用XSLT将XML转换为XML-转换类似的父节点
我有以下XML:使用XSLT将XML转换为XML-转换类似的父节点,xml,xslt,Xml,Xslt,我有以下XML: <EMPLOYEE_LIST> <EMPLOYEES_01> <PERMANENT> <EMPID>650000</EMPID> <FIRST_NAME>KEITH</FIRST_NAME> <MIDDLE_NAME>H</MIDDLE_NAME> &l
<EMPLOYEE_LIST>
<EMPLOYEES_01>
<PERMANENT>
<EMPID>650000</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>H</MIDDLE_NAME>
<LAST_NAME>ROGERS</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650001</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>Y</MIDDLE_NAME>
<LAST_NAME>HANNAH</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_01>
<EMPLOYEES_02>
<PERMANENT>
<EMPID>650002</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>ROGERS</MIDDLE_NAME>
<LAST_NAME>H</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650003</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>HANNAH</MIDDLE_NAME>
<LAST_NAME>Y</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_02>
</EMPLOYEE_LIST>
650000
基思
H
罗杰斯
650001
达里尔
Y
汉娜
650002
基思
罗杰斯
H
650003
达里尔
汉娜
Y
我使用以下XML对其进行转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/EMPLOYEE_LIST">
<employees>
<xsl:apply-templates select="EMPLOYEES/node()"/>
</employees>
</xsl:template>
<xsl:template match="PERMANENT">
<permanent>
<xsl:apply-templates select="*"/>
</permanent>
</xsl:template>
<xsl:template match="EMPID">
<emp_id>
<xsl:value-of select="."/>
</emp_id>
</xsl:template>
<xsl:template match="FIRST_NAME">
<f_name>
<xsl:value-of select="."/>
</f_name>
</xsl:template>
<xsl:template match="MIDDLE_NAME">
<m_name>
<xsl:value-of select="."/>
</m_name>
</xsl:template>
<xsl:template match="LAST_NAME">
<l_name>
<xsl:value-of select="."/>
</l_name>
</xsl:template>
<xsl:template match="CONTRACTUAL">
<permanent>
<xsl:apply-templates select="*"/>
</permanent>
</xsl:template>
<xsl:template match="EMPID">
<emp_id>
<xsl:value-of select="."/>
</emp_id>
</xsl:template>
<xsl:template match="FIRST_NAME">
<f_name>
<xsl:value-of select="."/>
</f_name>
</xsl:template>
<xsl:template match="MIDDLE_NAME">
<m_name>
<xsl:value-of select="."/>
</m_name>
</xsl:template>
<xsl:template match="LAST_NAME">
<l_name>
<xsl:value-of select="."/>
</l_name>
</xsl:template>
</xsl:stylesheet>
预期转换后的XML具有以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<emp_id>650000</emp_id>
<f_name>KEITH</f_name>
<m_name>H</m_name>
<l_name>ROGERS</l_name>
<type>permanent</type>
<emp_id>650001</emp_id>
<f_name>DARRYL</f_name>
<m_name>Y</m_name>
<l_name>HANNAH</l_name>
<type>contractual</type>
<emp_id>650002</emp_id>
<f_name>KEITH</f_name>
<m_name>ROGERS</m_name>
<l_name>H</l_name>
<type>permanent</type>
<emp_id>650003</emp_id>
<f_name>DARRYL</f_name>
<m_name>HANNAH</m_name>
<l_name>Y</l_name>
<type>contractual</type>
</employee>
</employees>
650000
基思
H
罗杰斯
永久的
650001
达里尔
Y
汉娜
契约的
650002
基思
罗杰斯
H
永久的
650003
达里尔
汉娜
Y
契约的
到目前为止,我还没有在这方面取得成功,因为我是新手,任何帮助都将不胜感激
谢谢我将使用Dimitre对您上一个问题的回答作为基础,因为它比我的更简洁:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:Renames>
<n old="EMPLOYEES" new="employees"/>
<n old="EMPID" new="emp_id"/>
<n old="FIRST_NAME" new="f_name"/>
<n old="MIDDLE_NAME" new="m_name"/>
<n old="LAST_NAME" new="l_name"/>
<n old="PERMANENT" new="permanent"/>
<n old="CONTRACTUAL" new="contractual"/>
</my:Renames>
<xsl:variable name="vRenames" select="document('')/*/my:Renames/*"/>
<xsl:template match="EMPLOYEE_LIST">
<employees>
<employee>
<xsl:apply-templates select="*/*" />
</employee>
</employees>
</xsl:template>
<xsl:template match="*/*" priority="-1">
<xsl:element name="{$vRenames[@old = name(current())]/@new}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="PERMANENT|CONTRACTUAL">
<xsl:apply-templates/>
<type>
<xsl:value-of select="$vRenames[@old = name(current())]/@new"/>
</type>
</xsl:template>
</xsl:stylesheet>
新的部分是:
<xsl:template match="EMPLOYEE_LIST">
<employees>
<employee>
<xsl:apply-templates select="*/*" />
</employee>
</employees>
</xsl:template>
在上面的示例输入上运行时,将生成:
<employees>
<employee>
<emp_id>650000</emp_id>
<f_name>KEITH</f_name>
<m_name>H</m_name>
<l_name>ROGERS</l_name>
<type>permanent</type>
<emp_id>650001</emp_id>
<f_name>DARRYL</f_name>
<m_name>Y</m_name>
<l_name>HANNAH</l_name>
<type>contractual</type>
<emp_id>650002</emp_id>
<f_name>KEITH</f_name>
<m_name>ROGERS</m_name>
<l_name>H</l_name>
<type>permanent</type>
<emp_id>650003</emp_id>
<f_name>DARRYL</f_name>
<m_name>HANNAH</m_name>
<l_name>Y</l_name>
<type>contractual</type>
</employee>
</employees>
650000
基思
H
罗杰斯
永久的
650001
达里尔
Y
汉娜
契约的
650002
基思
罗杰斯
H
永久的
650003
达里尔
汉娜
Y
契约的
一个更简短、更“推式”的解决方案:只需稍微调整我对上一个问题的回答:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:Renames>
<n old="EMPID" new="emp_id"/>
<n old="FIRST_NAME" new="f_name"/>
<n old="MIDDLE_NAME" new="m_name"/>
<n old="LAST_NAME" new="l_name"/>
<n old="PERMANENT" new="permanent"/>
<n old="CONTRACTUAL" new="contractual"/>
</my:Renames>
<xsl:variable name="vRenames" select="document('')/*/my:Renames/*"/>
<xsl:template match="EMPLOYEE_LIST">
<employees><employee><xsl:apply-templates/></employee></employees>
</xsl:template>
<xsl:template match="*/*/*" priority="-1">
<xsl:element name="{$vRenames[@old = name(current())]/@new}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="PERMANENT|CONTRACTUAL">
<xsl:apply-templates/>
<type><xsl:value-of select="$vRenames[@old = name(current())]/@new"/></type>
</xsl:template>
</xsl:stylesheet>
<EMPLOYEE_LIST>
<EMPLOYEES_01>
<PERMANENT>
<EMPID>650000</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>H</MIDDLE_NAME>
<LAST_NAME>ROGERS</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650001</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>Y</MIDDLE_NAME>
<LAST_NAME>HANNAH</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_01>
<EMPLOYEES_02>
<PERMANENT>
<EMPID>650002</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>ROGERS</MIDDLE_NAME>
<LAST_NAME>H</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650003</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>HANNAH</MIDDLE_NAME>
<LAST_NAME>Y</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_02>
</EMPLOYEE_LIST>
<employees>
<employee>
<emp_id>650000</emp_id>
<f_name>KEITH</f_name>
<m_name>H</m_name>
<l_name>ROGERS</l_name>
<type>permanent</type>
<emp_id>650001</emp_id>
<f_name>DARRYL</f_name>
<m_name>Y</m_name>
<l_name>HANNAH</l_name>
<type>contractual</type>
<emp_id>650002</emp_id>
<f_name>KEITH</f_name>
<m_name>ROGERS</m_name>
<l_name>H</l_name>
<type>permanent</type>
<emp_id>650003</emp_id>
<f_name>DARRYL</f_name>
<m_name>HANNAH</m_name>
<l_name>Y</l_name>
<type>contractual</type>
</employee>
</employees>
将此转换应用于提供的XML文档时:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:Renames>
<n old="EMPID" new="emp_id"/>
<n old="FIRST_NAME" new="f_name"/>
<n old="MIDDLE_NAME" new="m_name"/>
<n old="LAST_NAME" new="l_name"/>
<n old="PERMANENT" new="permanent"/>
<n old="CONTRACTUAL" new="contractual"/>
</my:Renames>
<xsl:variable name="vRenames" select="document('')/*/my:Renames/*"/>
<xsl:template match="EMPLOYEE_LIST">
<employees><employee><xsl:apply-templates/></employee></employees>
</xsl:template>
<xsl:template match="*/*/*" priority="-1">
<xsl:element name="{$vRenames[@old = name(current())]/@new}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="PERMANENT|CONTRACTUAL">
<xsl:apply-templates/>
<type><xsl:value-of select="$vRenames[@old = name(current())]/@new"/></type>
</xsl:template>
</xsl:stylesheet>
<EMPLOYEE_LIST>
<EMPLOYEES_01>
<PERMANENT>
<EMPID>650000</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>H</MIDDLE_NAME>
<LAST_NAME>ROGERS</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650001</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>Y</MIDDLE_NAME>
<LAST_NAME>HANNAH</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_01>
<EMPLOYEES_02>
<PERMANENT>
<EMPID>650002</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>ROGERS</MIDDLE_NAME>
<LAST_NAME>H</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650003</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>HANNAH</MIDDLE_NAME>
<LAST_NAME>Y</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_02>
</EMPLOYEE_LIST>
<employees>
<employee>
<emp_id>650000</emp_id>
<f_name>KEITH</f_name>
<m_name>H</m_name>
<l_name>ROGERS</l_name>
<type>permanent</type>
<emp_id>650001</emp_id>
<f_name>DARRYL</f_name>
<m_name>Y</m_name>
<l_name>HANNAH</l_name>
<type>contractual</type>
<emp_id>650002</emp_id>
<f_name>KEITH</f_name>
<m_name>ROGERS</m_name>
<l_name>H</l_name>
<type>permanent</type>
<emp_id>650003</emp_id>
<f_name>DARRYL</f_name>
<m_name>HANNAH</m_name>
<l_name>Y</l_name>
<type>contractual</type>
</employee>
</employees>
650000
基思
H
罗杰斯
650001
达里尔
Y
汉娜
650002
基思
罗杰斯
H
650003
达里尔
汉娜
Y
生成所需的正确结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:Renames>
<n old="EMPID" new="emp_id"/>
<n old="FIRST_NAME" new="f_name"/>
<n old="MIDDLE_NAME" new="m_name"/>
<n old="LAST_NAME" new="l_name"/>
<n old="PERMANENT" new="permanent"/>
<n old="CONTRACTUAL" new="contractual"/>
</my:Renames>
<xsl:variable name="vRenames" select="document('')/*/my:Renames/*"/>
<xsl:template match="EMPLOYEE_LIST">
<employees><employee><xsl:apply-templates/></employee></employees>
</xsl:template>
<xsl:template match="*/*/*" priority="-1">
<xsl:element name="{$vRenames[@old = name(current())]/@new}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="PERMANENT|CONTRACTUAL">
<xsl:apply-templates/>
<type><xsl:value-of select="$vRenames[@old = name(current())]/@new"/></type>
</xsl:template>
</xsl:stylesheet>
<EMPLOYEE_LIST>
<EMPLOYEES_01>
<PERMANENT>
<EMPID>650000</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>H</MIDDLE_NAME>
<LAST_NAME>ROGERS</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650001</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>Y</MIDDLE_NAME>
<LAST_NAME>HANNAH</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_01>
<EMPLOYEES_02>
<PERMANENT>
<EMPID>650002</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>ROGERS</MIDDLE_NAME>
<LAST_NAME>H</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650003</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>HANNAH</MIDDLE_NAME>
<LAST_NAME>Y</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_02>
</EMPLOYEE_LIST>
<employees>
<employee>
<emp_id>650000</emp_id>
<f_name>KEITH</f_name>
<m_name>H</m_name>
<l_name>ROGERS</l_name>
<type>permanent</type>
<emp_id>650001</emp_id>
<f_name>DARRYL</f_name>
<m_name>Y</m_name>
<l_name>HANNAH</l_name>
<type>contractual</type>
<emp_id>650002</emp_id>
<f_name>KEITH</f_name>
<m_name>ROGERS</m_name>
<l_name>H</l_name>
<type>permanent</type>
<emp_id>650003</emp_id>
<f_name>DARRYL</f_name>
<m_name>HANNAH</m_name>
<l_name>Y</l_name>
<type>contractual</type>
</employee>
</employees>
650000
基思
H
罗杰斯
永久的
650001
达里尔
Y
汉娜
契约的
650002
基思
罗杰斯
H
永久的
650003
达里尔
汉娜
Y
契约的
如果我将父节点名称从EMPLOYEES_02修改为Distanged_02,你的建议行吗?@ReggieMiller,是的,试试看:)我还没有对整个XML进行过尝试,但你能告诉我在转换过程中是否有方法截断/省略不需要的元素吗?假设我想在转换过程中截断l_名称,以便生成的xml只有f_名称/m_名称?这可以在代码中完成吗?@ReggieMiller,当然可以,但这是另一个新问题:)请参见我对这个问题的回答: