my question is about how to do this for n dates and 'fechas'(another template not allowed). I do it the hard way, I'm very new on the 'for-each' function. Thanks!
input xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transformer.xsl"?>
<dc>
<date>11</date>
<date>20</date>
<status>Available</status>
<status>Collected</status>
</dc>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="/dc/status">
<date>
<xsl:if test="position()=1">
<xsl:attribute name="fechas">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:for-each select="/dc/date">
<xsl:if test="position()=1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:if test="position()=2">
<xsl:attribute name="fechas">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:for-each select="/dc/date">
<xsl:if test="position()=2">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:if>
</date>
</xsl:for-each>
And this is my output:
<?xml version="1.0" encoding="UTF-8"?>
<date fechas="Available">11</date>
<date fechas="Collected">20</date>
You can try this:
<xsl:template match="/">
<xsl:for-each select="dc/date">
<xsl:variable name="pos" select="position()"/>
<xsl:copy>
<xsl:if test="../status[$pos]">
<xsl:attribute name="fechas">
<xsl:value-of select="../status[$pos]"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
See Transformation at http://xsltransform.net/pNvs5w4
What's wrong with this:
<xsl:template match="/">
<date fechas="{dc/status[1]}>
<xsl:value-of select="dc/date[1]"/>
</date>
<date fechas="{dc/status[2]}>
<xsl:value-of select="dc/date[2]"/>
</date>
</xsl:template>
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.