องค์ประกอบ <xsl:preserve-space> และ <xsl:strip-space> ของ XSLT
การกำหนดและการใช้งาน
<xsl:preserve-space> ถูกใช้เพื่อกำหนดองค์ประกอบที่ต้องรักษาความว่าง:
<xsl:strip-space> ถูกใช้เพื่อกำหนดองค์ประกอบที่ต้องลบความว่าง:
หมายเหตุ:การรักษาความว่างเป็นค่าปริยาย ดังนั้นเฉพาะเมื่อใช้ <xsl:strip-space> ควรใช้ <xsl:preserve-space> ด้วย:
หมายเหตุ:<xsl:preserve-space> และ <xsl:strip-space> คือองค์ประกอบระดับสูง (top-level element).
การใช้งาน
<xsl:preserve-space elements="list-of-element-names"/> <xsl:strip-space elements="list-of-element-names"/>
ขอบเขต
ขอบเขต | ค่า | การอธิบาย |
---|---|---|
elements | list-of-element-names |
ข้อบังคับ。รายชื่อองค์ประกอบแบบย่อยที่แยกด้วย пробел ที่กำหนดองค์ประกอบที่ต้องรักษาหรือลบความว่าง: หมายเหตุ: รายชื่อที่มี "*" และ "prefix:*" ในรายการ จะสามารถเสริมองค์ประกอบทั้งหมดหรือองค์ประกอบที่มาจากชื่อชนิดเฉพาะในชื่อเรียกแนวทาง: |
ตัวอย่าง
ตัวอย่าง 1
ในตัวอย่างนี้ เราจะให้ความสำคัญกับตัวเลือกที่ว่างให้กับองค์ประกอบ title และ artist และลบความว่างจากองค์ประกอบ country, company, price และ year:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:strip-space elements="country company price year" /> <xsl:preserve-space elements="title artist" /> <xsl:template match="/"> <html> <body> <xsl:for-each select="catalog/cd"> <p> <xsl:value-of select="title" /><br /> <xsl:value-of select="artist" /><br /> <xsl:value-of select="country" /><br /> <xsl:value-of select="company" /><br /> <xsl:value-of select="price" /><br /> <xsl:value-of select="year" /> </p> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>