我正在搜索xsl代码,该代码过滤给定xml数据文件的一些重要元素。以下示例显示了有关父亲及其两个孩子的全部数据。
此文件是我的输入文件:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<is_parent>true</is_parent>
<name>Sam</name>
<sex>male</sex>
<age>45</age>
<body_properties>
<heigth>183</heigth>
<weight>86</weight>
<eye_color>green</eye_color>
</body_properties>
<person>
<is_parent>false</is_parent>
<name>Julia</name>
<sex>female</sex>
<age>11</age>
<body_properties>
<heigth>155</heigth>
<weight>40</weight>
<eye_color>blue</eye_color>
</body_properties>
</person>
<person>
<is_parent>false</is_parent>
<name>Tom</name>
<sex>male</sex>
<age>4</age>
<body_properties>
<heigth>100</heigth>
<weight>35</weight>
<eye_color>brown</eye_color>
</body_properties>
</person>
</person>
有时我只想拥有最重要的&#34;信息。 例如,我需要名字,年龄和眼睛颜色。
这些数据必须在我的输出文件中:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name>Sam</name>
<age>45</age>
<body_properties>
<eye_color>green</eye_color>
</body_properties>
<person>
<name>Julia</name>
<age>11</age>
<body_properties>
<eye_color>blue</eye_color>
</body_properties>
</person>
<person>
<name>Tom</name>
<age>4</age>
<body_properties>
<eye_color>brown</eye_color>
</body_properties>
</person>
</person>
有时我只想拥有其他&#34;最重要的&#34;信息。 例如,我需要性别,身高和体重。当然,在这种情况下,我需要另一个xsl文件。
作为附加要求,xsl必须支持没有父项的输入数据文件,例如:
<person>
<is_parent>false</is_parent>
<name>Peter</name>
<sex>male</sex>
<age>23</age>
<body_properties>
<heigth>195</heigth>
<weight>99</weight>
<eye_color>blue</eye_color>
</body_properties>
</person>
这些示例已经过简化,以便解释我的要求。真正的xml文件有很多元素。
但是所有xml输入文件都具有不带子节点的结构:
<person>
...
</person>
或带孩子的结构
<person>
...
<person>
...
</person>
<person>
...
</person>
<person>
...
</person>
</person>
您对xsl 1.0版中的xslt解决方案有什么想法吗? 请不要提出更改输入文件格式的建议。我不可能。
答案 0 :(得分:0)
我向一位同事求助并得到了支持。 以下代码适用于我。 可以是匹配/人/人的模板和/人可以在没有“不要重复自己”的情况下实现吗?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" media-type="xml" />
<xsl:template match="/person/person">
<xsl:copy>
<xsl:copy-of select="name"/>
<xsl:copy-of select="age"/>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="/person">
<xsl:copy>
<xsl:copy-of select="name"/>
<xsl:copy-of select="age"/>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="body_properties">
<xsl:copy>
<xsl:copy-of select="eye_color"/>
</xsl:copy>
</xsl:template>
<!-- catch all unhandled elements -->
<xsl:template match="@*|node()" />
</xsl:stylesheet>