I have this XML file:
<messages>
<text>
See you next <corr form="week">wk</corr>.
</text>
<text>
Are you ready <number form="for">4</number> this <duplicate form="week">weeeek</duplicate> end<mark>?</mark>
</text>
</messages>
that I want to change into this one by using XSLT 1.0:
<file>
<messages>
<node>
<text>See you next </text>
<node>
<text>week</text>
</node>
</node>
</messages>
<messages>
<node>
<text>Are you ready </text>
<node>
<text> for</text>
<node>
<text> this</text>
<node>
<text> week</text>
<node>
<text> end</text>
<node>
<text> ?</text>
</node>
</node>
</node>
</node>
</node>
</node>
</messages>
</file>
In the second file, everytime we reach a tag with a "form" attribute, we have to create a new node with the form text in its own tag.
Here's my XSLT but it doesn't work well since the tags aren't at the right place. Help please?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:element name="file/messages">
<xsl:for-each select="text">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:element>
</xsl:template>
<!-- here, this doesn't work... -->
<xsl:template match="text">
<node><text>
<xsl:apply-templates/>
</text></node>
</xsl:template>
<xsl:template match="*[@form]">
<!-- gets the tag name -->
<xsl:variable name="attribute" select="local-name()"/>
<node><text>
<!-- print the form text -->
<xsl:if
test="$attribute='corr' or $attribute='number' or $attribute='duplicate'">
<xsl:value-of select="./@form"/>
</text></node>
</xsl:if>
<!-- print the "?" -->
<xsl:if
test="($attribute!='corr' and $attribute!='number' and $attribute!='duplicate'">
<xsl:value-of select="."/>
</text></node>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
If you are sure that's the output you want, try the following approach, known as " sibling recursion ":
XSLT 1.0
(edited to conform to the edited question)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="messages">
<file>
<xsl:apply-templates select="text"/>
</file>
</xsl:template>
<xsl:template match="text">
<message>
<xsl:apply-templates select="node()[1]"/>
</message>
</xsl:template>
<xsl:template match="text/*[@form]">
<node>
<text><xsl:value-of select="@form"/></text>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</node>
</xsl:template>
<xsl:template match="text/text() | text/*[not(@form)]">
<node>
<text><xsl:value-of select="."/></text>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</node>
</xsl:template>
</xsl:stylesheet>
When applied to the following test input :
<messages>
<text>See you next <corr form="week">wk</corr>.</text>
<text>Are you ready <number form="for">4</number> this <duplicate form="week">weeeek</duplicate> end<mark>?</mark></text>
</messages>
the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<file>
<message>
<node>
<text>See you next </text>
<node>
<text>week</text>
<node>
<text>.</text>
</node>
</node>
</node>
</message>
<message>
<node>
<text>Are you ready </text>
<node>
<text>for</text>
<node>
<text> this </text>
<node>
<text>week</text>
<node>
<text> end</text>
<node>
<text>?</text>
</node>
</node>
</node>
</node>
</node>
</node>
</message>
</file>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.