0

请有人可以帮助一个真正挣扎的新手。我是 XSL 的新手,我无法解决这个问题。我可以将其关闭,但无法对<Material_Data>,<Material><Comp_Class>标签进行排序。以下是源文件:

 <?xml version="1.0" encoding="UTF-8"?>
<Variable_Attributes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\ZZ\Test Var Attribs.xsd">
    <Material>1</Material>
    <Comp_Class>Aerosol - Aluminium</Comp_Class>
    <Material_Data>
        <Field_Name>ZMDM_Diameter</Field_Name>
        <Field_Value>45</Field_Value>
        <Field_Name>ZMDM_Height</Field_Name>
        <Field_Value>60</Field_Value>
        <Field_Name>ZMDM_Supplier</Field_Name>
        <Field_Value>Tony</Field_Value>
        <Field_Name>ZMDM_Brimful</Field_Name>
        <Field_Value>78</Field_Value>
        <Field_Name>ZMDM_Size</Field_Name>
        <Field_Value>440</Field_Value>
    </Material_Data>
    <Material>2</Material>
    <Comp_Class>Aerosol Valves</Comp_Class>
    <Material_Data>
        <Field_Name>ZMDM_UoM</Field_Name>
        <Field_Value>EA</Field_Value>
        <Field_Name>ZMDM_Free_Text</Field_Name>
        <Field_Value>Test Aerosol</Field_Value>
        <Field_Name>ZMDM_Size</Field_Name>
        <Field_Value>18</Field_Value>
    </Material_Data>
</Variable_Attributes>

我需要得到的是:

<?xml version="1.0" encoding="UTF-8"?>
<Material_Data>
    <Material>1</Material>
    <Comp_Class>Aerosol - Aluminium</Comp_Class>
    <ZMDM_Diameter>45</ZMDM_Diameter>
    <ZMDM_Height>60</ZMDM_Height>
    <ZMDM_Supplier>Tony</ZMDM_Supplier>
    <ZMDM_Brimful>78</ZMDM_Brimful>
    <ZMDM_Size>440</ZMDM_Size>
    <ZMDM_UoM>EA</ZMDM_UoM>
</Material_Data>
<Material_Data>
    <Material>2</Material>
    <Comp_Class>Aerosol Valves</Comp_Class>
    <ZMDM_Free_Text>Test Aerosol</ZMDM_Free_Text>
    <ZMDM_Size>18</ZMDM_Size>
</Material_Data>

任何帮助将非常感激。

提前致谢

史蒂夫

以下是我迄今为止尝试过的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <Material_Data>
            <xsl:for-each select="/Variable_Attributes/Material_Data/Field_Name">
                <xsl:variable name="val" select="current()"/>
                <xsl:variable name="val2" select="current()/following-sibling::*[1]"/>
                <xsl:element name="{$val}">
                    <xsl:value-of select="$val2"/>
                </xsl:element>
            </xsl:for-each>
        </Material_Data>
    </xsl:template>
</xsl:stylesheet>
4

1 回答 1

2

而不是使用xsl:for-each, 尝试使用基于模板的方法。

XSLT 1.0

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

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

    <xsl:template match="Variable_Attributes">
        <xsl:copy>
            <xsl:apply-templates select="Material_Data|@*"/>
        </xsl:copy>     
    </xsl:template>

    <xsl:template match="Material_Data">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="preceding-sibling::Material[1]"/>
            <xsl:apply-templates select="preceding-sibling::Comp_Class[1]"/>
            <xsl:apply-templates select="Field_Name"/>
        </xsl:copy>     
    </xsl:template>

    <xsl:template match="Field_Name">
        <xsl:element name="{.}">
            <xsl:apply-templates select="following-sibling::Field_Value[1]"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="Field_Value">
        <xsl:apply-templates select="node()"/>
    </xsl:template>

</xsl:stylesheet>

输出

<Variable_Attributes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:noNamespaceSchemaLocation="C:\ZZ\Test Var Attribs.xsd">
   <Material_Data>
      <Material>1</Material>
      <Comp_Class>Aerosol - Aluminium</Comp_Class>
      <ZMDM_Diameter>45</ZMDM_Diameter>
      <ZMDM_Height>60</ZMDM_Height>
      <ZMDM_Supplier>Tony</ZMDM_Supplier>
      <ZMDM_Brimful>78</ZMDM_Brimful>
      <ZMDM_Size>440</ZMDM_Size>
   </Material_Data>
   <Material_Data>
      <Material>2</Material>
      <Comp_Class>Aerosol Valves</Comp_Class>
      <ZMDM_UoM>EA</ZMDM_UoM>
      <ZMDM_Free_Text>Test Aerosol</ZMDM_Free_Text>
      <ZMDM_Size>18</ZMDM_Size>
   </Material_Data>
</Variable_Attributes>
于 2012-07-12T02:21:30.570 回答