XSLT将子节点文本转换为父节点的属性

时间:2015-11-14 23:16:04

标签: xml xslt

我试图使用XSL转换我的XML文档,以便某些子节点的文本值成为其父节点的属性。

以下是我所拥有的:

<?xml version="1.0" encoding="UTF-8"?>
<property_data>
  <property>
    <property_descriptions>
      <property_description>
        <type>property</type>
        <description>property 1000 description</description>
      </property_description>
      <property_description>
        <type>rate</type>
        <description>text rate description</description>
      </property_description>
    </property_descriptions>
    <property_attributes>
      <property_id>1000</property_id>
    </property_attributes>
  </property>
    <property>
...
  </property>
</property_data>

这就是我想要实现的目标:

 <?xml version="1.0" encoding="UTF-8"?>
 <property_data>
  <property>
    <property_descriptions>
      <property_description type="property">property 1000 description</property_description>
      <property_description type="rate">text rate description</property_descriptions>
    <property_attributes>
      <property_id>1000</property_id>
    </property_attributes>
  </property>
    <property>
...
  </property>
</property_data>

我被困在需要选择孩子价值的部分。

修改: 根据michael.hor257k的建议,我能够得到以下.xsl来完成这项工作:

<?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" version="1.0">

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

    <xsl:template match="property_description">
        <property_description type="{type}">
            <xsl:value-of select="description"/>
        </property_description>
    </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

这与 identity transform 模板一起应该提供预期的结果:

<xsl:template match="property_description">
    <property_description type="{type}">
        <xsl:value-of select="description"/>
    </property_description>
</xsl:template>
相关问题