这是与XSL转换相关的内容。输入.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="/x/transform.xsl"?>
<!-- input file root node-->
<message>
<header>
<!--Many descendents will be there -->
</header>
<body>
<!-- some other several elements will be there-->
<TaskList>
<TaskItem>
<DataPoint>
<Name>software.prog_name</Name>
<Target>JAVA</Target>
</DataPoint>
<DataPoint>
<Name>software.prog_rev</Name>
<Target>1</Target>
</DataPoint>
<DataPoint>
<Name>software.sw_product_id</Name>
<Target>1000</Target>
</DataPoint>
<DataPoint>
<Name>software.limits_file_name</Name>
<Target>limits.txt</Target>
</DataPoint>
<DataPoint>
<Name>software.limits_file_rev</Name>
<Target>2</Target>
</DataPoint>
<DataPoint>
<Name>hw_exp.class</Name>
<Target>Car</Target>
</DataPoint>
<DataPoint>
<Name>hw_exp.type</Name>
<Target>B</Target>
</DataPoint>
<DataPoint>
<Name>hw_exp.rev</Name>
<Target>32</Target>
</DataPoint>
<DataPoint>
<Name>prompt_id</Name>
<Target>100</Target>
</DataPoint>
</TaskItem>
<AutomationParam>
<Profile>Profile 1</Profile>
<SubGroup>1</SubGroup>
<Name>software.prog_name</Name>
<Value>JAVA</Value>
</AutomationParam>
<AutomationParam>
<Profile>Profile 1</Profile>
<SubGroup>1</SubGroup>
<Name>software.sw_product_id</Name>
<Value>1000</Value>
</AutomationParam>
<AutomationParam>
<Profile>Profile 1</Profile>
<SubGroup>2</SubGroup>
<Name>hw_exp.class</Name>
<Value>Animal</Value>
</AutomationParam>
<AutomationParam>
<Profile>Profile 1</Profile>
<SubGroup>2</SubGroup>
<Name>hw_exp.type</Name>
<Value>B</Value>
</AutomationParam>
<AutomationParam>
<Profile>Profile 1</Profile>
<SubGroup>3</SubGroup>
<Name>hw_exp.class</Name>
<Value>Flight</Value>
</AutomationParam>
<AutomationParam>
<Profile>Profile 1</Profile>
<SubGroup>3</SubGroup>
<Name>hw_exp.type</Name>
<Value>E</Value>
</AutomationParam>
<AutomationParam>
<Profile>Profile 1</Profile>
<SubGroup>3</SubGroup>
<Name>hw_exp.rev</Name>
<Value>1</Value>
</AutomationParam>
<AutomationParam>
<Profile>Profile 2</Profile>
<SubGroup>1</SubGroup>
<Name>software.sw_product_id</Name>
<Value>1000</Value>
</AutomationParam>
</TaskList>
</body>
</message>
在输入上应用 XSL 变形后.xml将输出生成的文件.xml输出.xml(应该是这样的)
<?xml version="1.0" encoding="utf-8"?>
<!-- output file root node-->
<text>
<header>
<!--All descendents will/should be copied as it is-->
</header>
<body>
<sub_body>
<!-- All such several elements from input.xml will/should be there-->
<test_profile>
<software>
<prog_name>JAVA</prog_name>
<prog_rev>1</prog_rev>
<sw_product_id>1000</sw_product_id>
<limits_file_name>limits.txt</limits_file_name>
<limits_file_rev>2</limits_file_rev>
</software>
<hw_exp>
<class>Car</class>
<type>B</type>
<rev>32</rev>
</hw_exp>
<prompt_id>100</prompt_id>
</test_profile>
<test_profile>
<software>
<prog_name>JAVA</prog_name>
<sw_product_id>1000</sw_product_id>
</software>
<hw_exp>
<class>Animal</class>
<type>B</type>
</hw_exp>
<hw_exp>
<class>Flight</class>
<type>E</type>
<rev>1</rev>
</hw_exp>
</test_profile>
<test_profile>
<software>
<sw_product_id>1000</sw_product_id>
</software>
</test_profile>
</sub_body>
</body>
请有人帮助我实现这一目标,这将是一个很大的帮助。我需要 XSL 1.0(但不是 2.x +)代码。
映射规则:a) 输出中的一条
注意:认为此输入.xml文件是根据数据库中的记录动态填充的。
这不是一个完整的解决方案。你的问题太不清楚了,不允许这样做。此 XSLT 1.0 样式表可能会为您提供有用的指示,以便您提供自己的解决方案。
这个样式表...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<xsl:copy>
<sub_body>
<test_profile>
<xsl:apply-templates select="TaskList/TaskItem"/>
</test_profile>
</sub_body>
</xsl:copy>
</xsl:template>
<xsl:template match="TaskItem">
<test_profile>
<software>
<prog_name><xsl:value-of select="DataPoint[Name='software.prog_name']/Target" /></prog_name>
<prog_rev><xsl:value-of select="DataPoint[Name='software.prog_re']/Target"/></prog_rev>
<sw_product_id><xsl:value-of select="DataPoint[Name='software.sw_product_id']/Target"/></sw_product_id>
<limits_file_name><xsl:value-of select="DataPoint[Name='software.limits_file_name']/Target"/></limits_file_name>
<limits_file_rev><xsl:value-of select="DataPoint[Name='software.limits_file_rev']/Target" /></limits_file_rev>
</software>
<hw_exp>
<class><xsl:value-of select="DataPoint[Name='hw_exp.class']/Target" /></class>
<type><xsl:value-of select="DataPoint[Name='hw_exp.type']/Target" /></type>
<rev><xsl:value-of select="DataPoint[Name='hw_exp.rev']/Target" /></rev>
</hw_exp>
<prompt_id><xsl:value-of select="DataPoint[Name='prompt_id']/Target" /></prompt_id>
</test_profile>
</xsl:template>
</xsl:stylesheet>
。应用于示例输入时,将生成所需输出的第一个test_profile节点。您尚未解释与其他两个test_profile节点相关的转换规则。
更新
此样式表将使用 muenchian 分组和运行时计算元素名称来生成其他test_profile节点,并按子组组织hw_exp节点。
这个样式表...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:key name="param-by-profile" match="AutomationParam" use="substring-after( Profile, 'Profile ')" />
<xsl:key name="subgroup" match="AutomationParam" use="SubGroup" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<xsl:copy>
<sub_body>
<xsl:apply-templates select="TaskList/TaskItem"/>
<xsl:for-each select="TaskList/AutomationParam[
generate-id(.) = generate-id( key('param-by-profile', substring-after( Profile, 'Profile '))[1])
]">
<xsl:sort data-type="number" select="substring-after( Profile, 'Profile ')" />
<xsl:call-template name="profile-from-param">
<xsl:with-param name="automations" select="key('param-by-profile',substring-after( Profile, 'Profile '))" />
</xsl:call-template>
</xsl:for-each>
</sub_body>
</xsl:copy>
</xsl:template>
<xsl:template match="TaskItem">
<test_profile>
<software>
<prog_name><xsl:value-of select="DataPoint[Name='software.prog_name']/Target" /></prog_name>
<prog_rev><xsl:value-of select="DataPoint[Name='software.prog_rev']/Target"/></prog_rev>
<sw_product_id><xsl:value-of select="DataPoint[Name='software.sw_product_id']/Target"/></sw_product_id>
<limits_file_name><xsl:value-of select="DataPoint[Name='software.limits_file_name']/Target"/></limits_file_name>
<limits_file_rev><xsl:value-of select="DataPoint[Name='software.limits_file_rev']/Target" /></limits_file_rev>
</software>
<hw_exp>
<class><xsl:value-of select="DataPoint[Name='hw_exp.class']/Target" /></class>
<type><xsl:value-of select="DataPoint[Name='hw_exp.type']/Target" /></type>
<rev><xsl:value-of select="DataPoint[Name='hw_exp.rev']/Target" /></rev>
</hw_exp>
<prompt_id><xsl:value-of select="DataPoint[Name='prompt_id']/Target" /></prompt_id>
</test_profile>
</xsl:template>
<xsl:template name="profile-from-param">
<xsl:param name="automations" />
<test_profile>
<software>
<xsl:apply-templates select="$automations[SubGroup=1]" />
</software>
<xsl:for-each select="$automations
[generate-id(.) = generate-id( key('subgroup', SubGroup)[1])]
[SubGroup >= 2]
">
<xsl:sort data-type="number" select="SubGroup" />
<xsl:call-template name="hw-group">
<xsl:with-param name="automations" select="key('subgroup', SubGroup)" />
</xsl:call-template>
</xsl:for-each>
</test_profile>
</xsl:template>
<xsl:template name="hw-group">
<xsl:param name="automations" />
<hw_exp>
<xsl:apply-templates select="$automations" />
</hw_exp>
</xsl:template>
<xsl:template match="AutomationParam">
<xsl:element name="{substring-after(Name,'.')}">
<xsl:value-of select="Value" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
。当应用于提供的示例输入时,将生成此文档...
<?xml version="1.0" encoding="utf-8"?>
<message>
<header>
<!--Many descendents will be there -->
</header>
<body>
<sub_body>
<test_profile>
<software>
<prog_name>JAVA</prog_name>
<prog_rev>1</prog_rev>
<sw_product_id>1000</sw_product_id>
<limits_file_name>limits.txt</limits_file_name>
<limits_file_rev>2</limits_file_rev>
</software>
<hw_exp>
<class>Car</class>
<type>B</type>
<rev>32</rev>
</hw_exp>
<prompt_id>100</prompt_id>
</test_profile>
<test_profile>
<software>
<prog_name>JAVA</prog_name>
<sw_product_id>1000</sw_product_id>
</software>
<hw_exp>
<class>Animal</class>
<type>B</type>
</hw_exp>
<hw_exp>
<class>Flight</class>
<type>E</type>
<rev>1</rev></hw_exp>
</test_profile>
<test_profile>
<software>
<sw_product_id>1000</sw_product_id>
</software>
</test_profile>
</sub_body>
</body>
</message>