xslt - Assign Attribute Value to Content of Element in XML (using XSD or XSL) -
i have document requires titles have standard names, while others left author. distinguish difference, have created attribute, "hardtitle". if hardtitle attribute has value, title element should display value of hardtitle , lock being edited. if hardtitle attribute empty, author can use whatever title like.
i have tried using enumeration values (code below), tell me if value incorrect - won't populate value in element nor lock element content being edited.
what like:
<chapter> <title hardtitle="scope">scope</title> [auto-populated hardtitle , locked] ... <title>this title can anything</title> ... <title hardtitle="newt">newt</title> [auto-populated hardtitle , locked] </chapter>
here code have far. know xs:restriction restricting text enumerated value... looking force content based on attribute , lock editing.
.xsd file snippet:
<xs:element name="title" type="editabletitle"> <xs:alternative test="if(@hardtitle)" type="lockedtitle" /> </xs:element> <xs:complextype name="editabletitle"> <xs:simplecontent> <xs:extension base="xs:string"> <xs:attribute name="hardtitle" /> </xs:extension> </xs:simplecontent> </xs:complextype> <xs:complextype name="lockedtitle"> <xs:simplecontent> <xs:restriction base="editabletitle"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:enumeration value="@hardtitle" /> </xs:restriction> </xs:simpletype> </xs:restriction> </xs:simplecontent> </xs:complextype>
your constraint can applied, in sense of converting input document different document wherein constraint satisfied, via simple xsl transform:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*" /> </xsl:copy> </xsl:template> <xsl:template match="title[@hardtitle]"> <xsl:copy> <!-- supposes <title> elements not have child elements --> <xsl:apply-templates select="@*" /> <xsl:value-of select="@hardtitle" /> </xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment