开发者

Xslt transformation

I'm going to convert the first XML into the second one using XSLT transformation

First:

<Data> 
 <Time> 
  <ID>IDvalue1</ID> 
  <field1>PropertyValue1</field1> 
  <field2>PropertyName1</field2> 
 </Time> 
 <Time>
  <ID>IDvalue2</ID> 
  <field1>PropertyValue2</field1> 
  <field2>PropertyName1</field2> 
 </Time>
 <Time> 
  <ID>IDvalue1</ID> 
  <field1>PropertyValue3</field1> 
  <field2> PropertyName2</field2> 
 </Time> 
 <Time> 
  <ID>IDvalue2</ID> 
  <field1>开发者_JAVA百科PropertyValue4</field1> 
  <field2>PropertyName2</field2> 
 </Time>
</Data>
....

Second:

<Data> 
 <Time> 
  <ID>IDvalue1</ID> 
  <PropertyName1>PropertyValue1</PropertyName1> 
  <PropertyName2>PropertyValue3</PropertyName2> 
 </Time> 
 <Time> 
  <ID>IDvalue2</ID> 
  <PropertyName1>PropertyValue2</ PropertyName1> 
  <PropertyName2>PropertyValue4</PropertyName2> 
 </Time> 
</Data>
.....

In the first XML there number of ID nodes which have same values. In the second XML they are compiled into a single nodes. After each ID in the first XML there are field1 and field2 nodes. In the second XML new nodes have to be created where field2 is tag name and field1 is value. These new nodes are gathered from all ID nodes with same value.

Could you help me to write XSLT code?


Use this template:

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

  <xsl:key name="k" match="Time" use="ID"/>

  <xsl:template match="Data">
    <Data>
      <xsl:apply-templates select="Time[generate-id(.) = generate-id(key('k', ID))]"/>
    </Data>
  </xsl:template>

  <xsl:template match="Time">
    <Time>
      <xsl:copy-of select="ID"/>
      <xsl:for-each select="key('k', ID)">
        <xsl:element name="{normalize-space(field2)}">
          <xsl:value-of select="field1"/>
        </xsl:element>
      </xsl:for-each> 
    </Time>
  </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="utf-8"?>
<Data>
  <Time>
    <ID>IDvalue1</ID>
    <PropertyName1>PropertyValue1</PropertyName1>
    <PropertyName2>PropertyValue3</PropertyName2>
  </Time>
  <Time>
    <ID>IDvalue2</ID>
    <PropertyName1>PropertyValue2</PropertyName1>
    <PropertyName2>PropertyValue4</PropertyName2>
  </Time>
</Data>


This is a standard grouping question. You'll find masses of help if you search for "XSLT grouping". Grouping is very easy in XSLT 2.0 using the xsl:for-each-group instruction, but quite tricky in XSLT 1.0, as polishchuk has demonstrated. (Personally I don't even attempt to answer grouping questions without first knowing which version of XSLT you are using and why - the code is so different in the two cases.)


Microsoft .NET framework does not yet support XSLT 2.0. BUT, If you are going to use XSLT from ASP .NET you can easily implement (@Michael Key) Saxon. See this topic for more information.

When you are ready, your grouping solution will be very simple. Here an XSLT 2.0 grouping example applied on your question:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:template match="Data">
        <Data>
            <xsl:for-each-group select="Time" group-by="ID">
                <Time>
                    <xsl:copy-of select="ID"/>
                    <xsl:apply-templates select="current-group()"/>
                </Time>
            </xsl:for-each-group>
        </Data>
    </xsl:template>

    <xsl:template match="Time">
        <xsl:element name="{field2}">
            <xsl:value-of select="field1"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜