xslt用于在xml中为我的属性名称添加前缀

时间:2013-04-25 01:16:54

标签: xml xslt

我有一个这种格式的xml文档。

<SampleXMLFormat>
<Header>
<Id>123</Id>
</header>
<Properties>
<property name= "type" value = "a1">
<property name="prop1" value="val1"/>
<property name="prop2" value="val2"/>
</Properties>
<Properties>
<property name= "type" value = "a2">
<property name="prop1" value="val1"/>
<property name="prop2" value="val2"/>
</Properties>
</SampleXMLFormat>

我无法编写将xml文档转换为

之类的xslt转换
<SampleXMLFormat>
<Header>
<Id>123</Id>
</Header>
<Properties>
<property name="a1_prop1" value="val1"/>
<property name="a1_prop2" value="val2"/>
<property name="a2_prop1" value="val1"/>
<property name="a2_prop2" value="val2"/>
</Properties>
</SampleXMLFormat>

请问,我可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <SampleXMLFormat>
    <xsl:copy-of select="Header"/>
    <Properties>
     <xsl:apply-templates/>
    </Properties>
  </SampleXMLFormat>
 </xsl:template>

 <xsl:template match="property[not(@name='type')]">
  <property name="{../property[@name='type']/@value}_{@name}" value="{@value}"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的XML文档时:

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name= "type" value = "a1"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
  <Properties>
    <property name= "type" value = "a2"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
</SampleXMLFormat>

会产生想要的正确结果:

<SampleXMLFormat>
   <Header>
      <Id>123</Id>
   </Header>
   <Properties>
      <property name="a1_prop1" value="val1"/>
      <property name="a1_prop2" value="val2"/>
      <property name="a2_prop1" value="val1"/>
      <property name="a2_prop2" value="val2"/>
   </Properties>
</SampleXMLFormat>

<强>解释

正确使用:

  1. <强> Template match patterns

  2. AVT s(属性值模板)。

答案 1 :(得分:1)

您的XML格式不正确,但假设这是您的意思(请注意已终止的“类型”属性):

<SampleXMLFormat>
  <Properties>
    <property name= "type" value = "a1"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
  <Properties>
    <property name= "type" value = "a2"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
</SampleXMLFormat>

然后这个XSLT应该做到这一点:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

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

  <xsl:template match="/*">
    <xsl:copy>
      <Properties>
        <xsl:apply-templates
          select="Properties/*[not(self::property and 
                                   @name = 'type')]" />
      </Properties>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="property/@name">
    <xsl:attribute name="name">
      <xsl:value-of 
        select="concat(../../property[@name = 'type']/@value, '_', .)"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

在上述输入上运行时,结果为:

<SampleXMLFormat>
  <Properties>
    <property name="a1_prop1" value="val1" />
    <property name="a1_prop2" value="val2" />
    <property name="a2_prop1" value="val1" />
    <property name="a2_prop2" value="val2" />
  </Properties>
</SampleXMLFormat>