How to convert xml tag using xsl

2019-08-11 11:28发布

I need to convert an xml using xsl file, the question is: I have several tag with the same name but different attribute, I've to convert them changing their name with the name of their attribute and print their value. Here the sample:

<INDEX_FIELDS>
  <FIELD NAME= "Field1" VALUE= "value1"/>
  <FIELD NAME= "Field2" VALUE= "value2"/>
  <FIELD NAME= "Field3" VALUE= "value3"/>
  <FIELD NAME= "Field4" VALUE= "value4"/>
</INDEX_FIELDS>

I want make it like this sample:

<INDEX_FIELD>
  <FIELD1>VALUE1</FIELD1>
  <FIELD2>VALUE2</FIELD2>
  <FIELD3>VALUE3</FIELD3>
  <FIELD4>VALUE4</FIELD4>
</INDEX_FIELD>

I could use only xsl transformation. Someone can give me any help? I've visited W3C school site, tried several method but nothing seem's to work. Thanks to anyone will answer.

Here my xslt:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
  <ROOT>
   <HEAD>
    <TAG><xsl_value-of select="ROOT/HEAD/TAG"/></TAG>
     <-- Several tag -->
    </HEAD>
    <BODY>
    <DOCUMENTS><--Here it is a list of documents -->
    <xsl:for-each select="ROOT/BODY/DOCUMENTS/DOCUMENT">
    <DOCUMENT>
     <xsl:for-each select="INDEX_FIELDS/FIELD">
    enter code here
     </xsl:for-each>
    <-- Closing tags --> 

标签: xml xslt
2条回答
够拽才男人
2楼-- · 2019-08-11 11:51
<xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="FIELD">
        <xsl:element name="{@NAME}">
            <xsl:value-of select="@VALUE"/>
        </xsl:element>
    </xsl:template>
Try it.
查看更多
家丑人穷心不美
3楼-- · 2019-08-11 11:53

There is no need to use for-each loops. You can also use xsl:template match rules that match one part of your XML document. This rule will be executed every time a match occurs in the XML

Alternative way to structure your XSLT:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
  <ROOT>
   <HEAD>
    <TAG><xsl_value-of select="ROOT/HEAD/TAG"/></TAG>
     <-- Several tag -->
    </HEAD>
    <BODY>
    <DOCUMENTS>
        <xsl:apply-templates match="DOCUMENT"/>--this tells the XSLT processor to find all DOCUMENT nodes and process them. 
    <-- Closing tags --> 
....
<xsl:template match="DOCUMENT">
    <DOCUMENT>
        process your Document nodes here
        <xsl:apply-templates/> --this makes sure the subnodes in the DOCUMENT node are processed. 
    </DOCUMENT>
</xsl:template>
....
<xsl:template match="FIELD">
    <xsl:element name="{@NAME}">
        <xsl:value-of select="@VALUE"/>
    </xsl:element>
</xsl:template>

XSLT tutorial: http://edutechwiki.unige.ch/en/XSLT_Tutorial_-_Basics#A_first_glance_at_XSLT

XSLT works a bit differently to many programming languages: instead of a list of instructions with subroutines etc, you specify a list of possible nodes in your source document and what to do with them.

查看更多
登录 后发表回答