XSLT- Changing XML format and reusability
I could use some assistance with my XML transformation, I've viewed other similar questions but since my XML schema is a little awkward I'm having trouble applying it.
Here is the XML format I have
<root>
<row>
<var name="Name" value="Rusell" />
<var name=" Surname" value=" Anthony" />
<var name=" Country" value=" UK" />
&开发者_如何学编程lt;var name=" Job" value="Web Designer" />
<var name=" Cabin" value="345" />
</row>
<row>
<var name="Name" value="Wolf" />
<var name=" Surname" value=" Werner" />
<var name=" Country" value=" Germany" />
<var name=" Job" value="Linux IT" />
<var name=" Cabin" value="234" />
</row>
</root>
And this is what I want it to look like so I can access fields easily.
<root>
<row name="Rusell" surName="Anthony" country="UK" job="Web Designer" cabin="345" />
<row name="Wolf" surname="Werner" country="Germany" job="Linux IT" cabin="234" />
</row>
I have made progress as far as the formatting goes this is what I have got so far- The values have not been inserted.
<root>
<row Name="" Surname="" Country="" Job="" Cabin="" />
<row Name="" Surname="" Country="" Job="" Cabin="" />
<row Name="" Surname="" Country="" Job="" Cabin="" />
<row Name="" Surname="" Country="" Job="" Cabin="" />
</root>
Here is my XSLT file - You will notice for a couple of the attributes, when using value-of select I have different syntax this is just to show what I have tried.
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="row">
<row>
<xsl:attribute name="Name">
<xsl:value-of select="value"/>
</xsl:attribute>
<xsl:attribute name="Surname">
<xsl:value-of select="row/root/name/value"/>
</xsl:attribute>
<xsl:attribute name="Country">
<xsl:value-of select="root/row/value"/>
</xsl:attribute>
<xsl:attribute name="Job">
<xsl:value-of select="@value"/>
</xsl:attribute>
<xsl:attribute name="Cabin">
<xsl:value-of select="value"/>
</xsl:attribute>
</row>
</xsl:template>
Also, about the re usability issue, the above method is fine when I know the fields and attributes but I will be taking in different XML files with possibly more fields i.e address,age.
So how would I go about creating an XSLT file that does the following;
For each Node = root/row
Create row = (value@name & value@value)
I.E the same as what I'm trying to do above but reusable as in I don't need to know the name values (surname,cabin etc) so its applicable to my first XML file and the following.
<root>
<row>
<var name="differentName" value="x" />
<var name="anotherdifferentname" value="y" />
</row>
</root>
If I need to be clearer please let me know and thanks in advance.
This should do it
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="row">
<row>
<xsl:apply-templates select="var"></xsl:apply-templates>
</row>
</xsl:template>
<xsl:template match="var">
<xsl:variable name="attributeName" select="@name">
</xsl:variable>
<xsl:attribute name="{$attributeName}">
<xsl:value-of select="@value"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
精彩评论