XML/XSLT stylesheet/attribute set
Содержание
Add one more attribute to group and override the value of another
File: Data.xml
<poem year="1667" type="epic">
<verse>line 3</verse>
<verse>line 4</verse>
</poem>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="yes" />
<xsl:attribute-set name="lineAttrs">
<xsl:attribute name="status">done</xsl:attribute>
<xsl:attribute name="hue">
<xsl:value-of select="@color" />
</xsl:attribute>
<xsl:attribute name="number">
<xsl:value-of select="amount" />
</xsl:attribute>
<xsl:attribute name="sourceElement">
<xsl:text>src</xsl:text><xsl:value-of select="generate-id()" />
</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="verse">
<xsl:element name="line" use-attribute-sets="lineAttrs">
<xsl:attribute name="author">BD</xsl:attribute>
<xsl:attribute name="hue">NO COLOR</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<line status="done" hue="NO COLOR" number="" sourceElement="srcd2e3" author="BD">line 3</line>
<line status="done" hue="NO COLOR" number="" sourceElement="srcd2e6" author="BD">line 4</line>
Create two attribute sets
File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<message>test</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:attribute-set name="para">
<xsl:attribute name="doc:style"
namespace="http://www.wbex.ru/documents">classic</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="paragraph">
<xsl:attribute name="priority">medium</xsl:attribute>
<xsl:attribute name="date">2003-09-23</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="/">
<xsl:element name="paragraph" use-attribute-sets="para">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph xmlns:doc="http://www.wbex.ru/documents" doc:style="classic">test</paragraph>
Define and use attribute-set
File: Data.xml
<?xml version="1.0"?>
<countries>
<country name="France" />
<country name="Germany" />
<country name="Israel" />
<country name="Japan" />
<country name="Poland" />
<country name="United States" selected="yes" />
<country name="Venezuela" />
</countries>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="line">
<xsl:copy use-attribute-sets="sequence">
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:attribute-set name="sequence">
<xsl:attribute name="number" select="position()" />
<xsl:attribute name="of" select="last()" />
</xsl:attribute-set>
</xsl:transform>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<countries>
<country/>
<country/>
<country/>
<country/>
<country/>
<country/>
<country/>
</countries>
use-attribute-sets
File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<message>Message.</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:attribute-set name="para" use-attribute-sets="paragraph" />
<xsl:attribute-set name="paragraph">
<xsl:attribute name="priority">medium</xsl:attribute>
<xsl:attribute name="date">2003-09-23</xsl:attribute>
<xsl:attribute name="doc:style" namespace="http://www.wbex.ru/documents">classic</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="/">
<xsl:element name="paragraph" use-attribute-sets="para">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph xmlns:doc="http://www.wbex.ru/documents" priority="medium" date="2003-09-23"
doc:style="classic">Message.</paragraph>