【发布时间】:2012-07-04 00:21:40
【问题描述】:
这与 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>
在 input.xml 上应用 XSL 转换后,生成的文件为 output.xml output.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) output.xml 中的一个
注意:认为这个 input.xml 文件是根据数据库中的记录动态填充的。
【问题讨论】:
-
您好,如果问题不清楚,请在此处评论,我会尝试详细说明...但我特意寻找合乎逻辑的想法,因为它有点棘手..
-
您的问题与其说是一个问题,不如说是一个“你能为我做这件事”的问题。你能告诉我们你的一些尝试吗?您是否完全不熟悉 XSLT?如果是这样,互联网上充满了很棒的教程。 W3Schools 受到了很多批评,但我仍然觉得这是最好的起点:w3schools.com/xsl。如果您有任何其他问题,请尝试并回来!
-
@SandyGifford 我尝试了所有我能做的基本事情。实际上我对 XSL 很陌生。我尝试在某种程度上进行编码。我被困在 //DataPoint.Name 的 prompt_id 值。它没有“。”在里面。所以对我来说使用模板变得有点复杂。但是,无论谁保留答案,我也在寻求解释。我知道这可以通过使用两遍变换来完成,但我没有得到任何解决方案/想法来实现这一点(但我可以说这很棘手,需要正确理解这个问题)
-
如果你能发布你目前拥有的 XSLT 还是很好的,这样我们就可以从你所在的地方为你提供帮助。
-
你需要解释一下变换的规则。这些不能简单地从您当前的示例文档中推断出来。例如,有 3 个输出 test_profile 节点。关于最后两个来自哪里没有明显的规则。可以有超过 3 个 test_profile 节点吗?如果有,它取决于什么?
标签: xml xslt xml-parsing xslt-1.0 xalan