score:0
are you looking for something like this:
<xsl:template match="tag">
<xsl:element name="genre">
<xsl:value-of select="@name"/>
</xsl:element>
</xsl:template>
score:0
i had the same need in vb9 and wanted to go with linq instead of xsl. i found this: http://msdn.microsoft.com/en-us/library/bb669154.aspx and it worked really well.
score:1
you can match any node with node()
, like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<!-- uncomment to remove tags elements -->
<!-- <xsl:template match="tags" /> -->
<xsl:template match="genres">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<xsl:for-each select="../tags/tag">
<xsl:element name="genre">
<xsl:value-of select="@name" />
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<!-- default rule: copy node and descend -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
score:2
using one of the most fundamental and powerful xslt design patterns: overriding the identity template, one will write this very simple transformation to replace every "genres" element with a "topics" element:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="genres"> <topics> <xsl:apply-templates select="node()|@*"/> </topics> </xsl:template> </xsl:stylesheet>
when applied against the provided source xml document:
<collection> <dvd> <id>0000502461</id> <title>cirque du soleil: alegría</title> <released>2002-05-31</released> <runningtime>90</runningtime> <genres> <genre>family</genre> <genre>music</genre> </genres> <overview>what if anything were possible? what if ... </overview> <notes/> <tags> <tag name="kids" fullname="kids"/> </tags> </dvd> </collection>
the wanted result is produced:
<collection> <dvd> <id>0000502461</id> <title>cirque du soleil: alegría</title> <released>2002-05-31</released> <runningtime>90</runningtime> <topics> <genre>family</genre> <genre>music</genre> </topics> <overview>what if anything were possible? what if ... </overview> <notes/> <tags> <tag name="kids" fullname="kids"/> </tags> </dvd> </collection>
the first template in the stylesheet is the identity rule. it copies any matched node unchanged and recursively applies templates to its attributes or children. if no other template is present, this template creates identical copy of the source xml document, hence its name.
when there is a more specific template (specifying more specific details for the matched node, such as name and/or other conditions), it is said to "override" the more generic templates. this means that the more specific template is chosen for processing the node.
using this extremely powerful design pattern it is trivial to implementin just a few lines such processing as:
- delete all nodes that satisfy some condition.
- rename all nodes that satisfy some condition.
- modify the contents of all nodes that satisfy some condition
while copying all other nodes intact.
in our case, the second template is more specific and it gets selected for processing of every element named "genres". all it does is create an element named "topics" and inside it apply templates to all of the current node attributes and children.
finally, the following transformation will add a new "genre" element to the children of "genres", for each "tag" element:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="genres"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> <xsl:apply-templates select="../tags/tag" mode="gen"/> </xsl:copy> </xsl:template> <xsl:template match="tag" mode="gen"> <genre> <xsl:value-of select="@name"/> </genre> </xsl:template> </xsl:stylesheet>
the result is again as required:
<collection> <dvd> <id>0000502461</id> <title>cirque du soleil: alegría</title> <released>2002-05-31</released> <runningtime>90</runningtime> <genres> <genre>family</genre> <genre>music</genre>
<genre>kids</genre>
</genres> <overview>what if anything were possible? what if ... </overview> <notes/> <tags> <tag name="kids" fullname="kids"/> </tags> </dvd> </collection>
more code snippets using the "identity rule" pattern can be found here.
Source: stackoverflow.com
Related Query
- LINQ or XSLT to turn one Element into another in Visual Basic 9
- How can I combine this code into one or two LINQ queries?
- linq - how do you do a query for items in one query source that are not in another one?
- Linq & C# - Inserting distinct data from one class into another
- How can I code numerous MIN functions into one LINQ to DataSet query
- LINQ - How to select one element based on another element that exists in only some records
- Linq assign values from one table into another
- Linq remove elements from one collection into another collection
- Need help transforming one kind of list into another kind of list using LINQ
- Using LINQ to parse one XML file into multiple files based on child element value
- Combining parts of one list into another List LINQ
- Turn this code into LINQ
- How can i Insert into one table and Update another table using Linq
- How to use one Column of Sql query into another sql query inside C# Code
- Linq or XSLT combine multiple variable elements into one
- Inserting into database LINQ to SQL Visual Basic Visual Studio 2008
- Use LINQ to get items in one List<>, that are not in another List<>
- Convert string[] to int[] in one line of code using LINQ
- Linq code to select one item
- LINQ - Find all items in one list that aren't in another list
- Create a list of one object type from a list of another using Linq
- Linq Select Certain Properties Into Another Object?
- Using LINQ to Objects to find items in one collection that do not match another
- Merge two List<object> into one List in Linq
- Flatten List<string[]> into single string with one line for each element
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- Merge multiple Lists into one List with LINQ
- LINQ - array property contains element from another array
- check whether a List<string> contains an element in another List<string> using LINQ
- Turn a 2D grid into a 'diamond' with LINQ - is it possible?
More Query from same tag
- InvalidOperationException: The LINQ expression for groupby
- C# How to declare new Dictionary object based off a string array within a LINQ query
- C# : Checking if an object (who has a min and max property) falls between any existing objects? Explained inside
- Return value or null, without null reference exception linq
- LINQ Distinct not using the IEqualityComparer?
- Dynamic Columns from List using LINQ
- Using navigation properties in LINQ query causes problems such as relationship multiplicity
- Linq-to-sql Not Contains or Not in?
- Does LINQ load all items from SQL Server into memory or chunks only?
- Emulating a join which uses a contains operator opposed to equals?
- Sitecore Custom Index - WARN Could not map index document (field: _uniqueid
- Retrieve Row Index From DataView using LINQ
- C# linq Select objects in list looking inside another list of objects
- Make JOIN query match even if row being joined does not exist
- Remove parents from list
- Linq: Join 2 tables that share one column name (with different data)
- c# constraints ignored in extension with generic type
- How to Optmize a multiple if else with LINQ
- Bind linq data to dropdownlist
- Error 2 '' does not contain a constructor that takes 1 arguments, (Topic: Typed Context in Linq to SQL )
- Lambda Expression joining another table for average & sort
- Check string pattern in LINQ query
- How to convert data from two different locations in JSON file to a key(string) and value(double) within a C# dictionary, using Linq?
- pagination in asp.net mvc
- Visual Studio Code Analysis Rule - "Do not expose generic lists"
- Convert function to linq statment
- Is this conversion from SQL to LINQ correct?
- Dynamic query using linq
- LINQ no delayed loading
- Linq: split list based on property value