'Move sibling node into current node with XSL template
I'm new to XSL Templates and am attempting what I thought would be a simple transformation, but I am not getting very far. I want to transform this XML:
<Books>
<Name>
<p>Romeo and Juliet</p>
</Name>
<Text>
<p>Two houses...</p>
</Text>
<Name>
<p>Hamlet</p>
</Name>
<Text>
<p>Who's there...</p>
</Text>
</Books>
to this:
<Books>
<Book>
<Title>Romeo and Juliet</Title>
<Content>
<p>Two houses...</p>
</Content>
</Book>
<Book>
<Title>Hamlet</Title>
<Content>
<p>Who's there...</p>
</Content>
</Book>
</Books>
Note the following changes:
- Group the
Nameelement and its proceedingTextelement into aBookelement and rename them toTitleandContent. - Strip the
ptag from theName, but not from theText.
Here is my latest sorry attempt, which is not working:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
<xsl:template match="Name">
<Book>
<Title>
<xsl:copy-of select="."/>
</Title>
<Content>
<xsl:copy select="following-sibling::Text[1]"/>
</Content>
</Book>
</xsl:template>
<xsl:template name="gettitle" match="p">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Any help would be appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
