【发布时间】:2017-05-24 13:42:48
【问题描述】:
我需要使用 xslt 将一个 xml 文件转换为另一个 xml 文件。 输入文件如下:
<root type="object">
<items type="array">
<item type="object">
<updated_at type="string">2017-05-20</updated_at>
<external_id type="null"></external_id>
<created_at type="string">2017-05-20</created_at>
<embed_code type="string">ec111</embed_code>
<status type="string">live</status>
<content_type type="string">video</content_type>
<name type="string"></name>
<hosted_at type="null"></hosted_at>
</item>
<item type="object">
<updated_at type="string">2017-05-19</updated_at>
<external_id type="null"></external_id>
<labels type="array">
<item type="object">
<name type="string">lbl name 1</name>
<full_name type="string">full lbl name 1</full_name>
<id type="string">lbl1</id>
<parent_id type="string">parent_id_1</parent_id>
</item>
</labels>
<created_at type="string">2017-05-20</created_at>
<embed_code type="string">ec112</embed_code>
<status type="string">live</status>
<content_type type="string">video</content_type>
<name type="string">test</name>
<hosted_at type="null"></hosted_at>
</item>
<item type="object">
<updated_at type="string">2017-05-19</updated_at>
<external_id type="null"></external_id>
<labels type="array">
<item type="object">
<name type="string">lbl name 2</name>
<full_name type="string">full lbl name 2</full_name>
<id type="string">lbl2</id>
<parent_id type="string">parent_id_2</parent_id>
</item>
<item type="object">
<name type="string">lbl name 3</name>
<full_name type="string">full lbl name 3</full_name>
<id type="string">lbl3</id>
<parent_id type="string">parent_id_3</parent_id>
</item>
</labels>
<created_at type="string">2017-05-20</created_at>
<embed_code type="string">ec113</embed_code>
<status type="string">live</status>
<content_type type="string">video</content_type>
<name type="string">testing</name>
<hosted_at type="null"></hosted_at>
</item>
</items>
</root>
需要把上面的xml转换成下面的格式:
<?xml version="1.0"?>
<labels>
<item>
<embed_code>ec112</embed_code>
<name>lbl name 1</name>
<full_name>full lbl name 1</full_name>
<id>lbl1</id>
<parent_id>parent_id_1</parent_id>
</item>
<item>
<embed_code>ec113</embed_code>
<name>lbl name 2</name>
<full_name>full lbl name 2</full_name>
<id>lbl2</id>
<parent_id>parent_id_2</parent_id>
</item>
<item>
<embed_code>ec113</embed_code>
<name>lbl name 3</name>
<full_name>full lbl name 3</full_name>
<id>lbl3</id>
<parent_id>parent_id_3</parent_id>
</item>
</labels>
路径 root/items/item 将具有 embed_code 标记。在同一路径中,它可能有标签节点,也可能没有标签。标签可能有一项或多项。
转换规则如下: 1)需要得到标签和他们的孩子 2)删除所有属性 3) 将 embed_code 节点带入每个标签/项目节点
以下是我目前的代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="item">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<labels>
<xsl:for-each select="root/items/item">
<xsl:variable name="eCode" select="embed_code"/>
<xsl:for-each select="labels/*">
<item>
<embed_code>
<xsl:value-of select="$eCode"/>
</embed_code>
<xsl:for-each select="root/items/item/labels/item/*">
</xsl:for-each>
</item>
</xsl:for-each>
</xsl:for-each>
</labels>
</xsl:template>
<xsl:template match="@*" />
</xsl:stylesheet>
无法带入标签/项目的子节点。请帮我解决这个问题。
谢谢
【问题讨论】:
-
不要将工作任务转储到 Stack Overflow。做你自己的工作。如果您对当前正在编写的 XSLT 样式表有任何疑问,请发布您的代码并提出具体问题,我们很乐意为您提供帮助。
-
到目前为止,我已经编辑了这篇文章,以包含我的 xslt 工作。我要带labels/item的子节点。
-
这样更好。始终包含您的代码和问题描述。