การทำงานกับ XML คุณสมบัติโหนดใน XSLT

หากคุณใช้ XML และ XSL แล้วคุณอาจได้เจอเวลาเมื่อคุณต้องเล่นรอบที่มีคุณลักษณะและค่าของโหนด XML ในคุณ XSL พวกเขาจะโหลดของเว็บไซต์ที่มีข้อมูลการยืดยาวเกี่ยวกับเรื่องนี้ แต่ไม่มีผู้ใดผมพบว่ามีช่วงสั้น ๆ และแม่นยำ ... นี้ไม่มีสอน XML / XSL แต่ความพยายามของฉันที่จะนำร่วมกันจัดเรียงของรายการโกงบางส่วน ...

ตัวอย่าง XML ที่เราจะทำงานร่วมกับมีลักษณะเช่นนี้ ...

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl"?>
<food>
<fruits type="tropical">
<item name="mango" moreinfo="http://www.mango.com">Mango is the king of fruits</item>
<item name="banana" moreinfo="http://www.banana.com">Banana once a day , keeps the doctor away</item>
<item name="orange" moreinfo="http://www.orange.com">Orange is the color and rich in vitamin C</item>
<item name="Papaya" moreinfo="http://www.papaya.com">Papaya - Hot when raw, cold when ripe</item>
</fruits>
</food>

เพื่อช่วยให้เริ่มต้นการเปลี่ยนแปลงข้างต้น XML ของเราโดยใช้ XSL

ตัวอย่างที่ 1: แสดงค่าที่เลือกแอตทริบิวต์

<xsl:value-of select="/food/fruits/item[@name='orange']" /><br />
to get more information about <a href="{/food/fruits/item[@name='orange']/@moreinfo}" target="new" ><xsl:value-of select="/food/fruits/item[@name='orange']/@name" /> </a>

ผล HTML จะมีลักษณะเช่น

Orange is the color and rich in vitamin C,
to get more information about <a href="http://www.orange.com" target="new">


ตัวอย่างที่ 2: looping ผ่านและแสดงชื่อแอตทริบิวต์ทั้งหมด XML และคุ้มที่สุดของพวกเขา

<xsl:for-each select="/food/fruits/item/@*">
attribute name : <xsl:value-of select="name()"/>,
attribute value : <xsl:value-of select="."/> <br />
</xsl:for-each>

ผล HTML จะมีลักษณะเช่น


attribute name : name, attribute value : mango
attribute name : moreinfo, attribute value : http://www.mango.com
attribute name : name, attribute value : banana
attribute name : moreinfo, attribute value : http://www.banana.com
attribute name : name, attribute value : orange
attribute name : moreinfo, attribute value : http://www.orange.com
attribute name : name, attribute value : Papaya
attribute name : moreinfo, attribute value : http://www.papaya.com


ดาวน์โหลดไฟล์ทั้งหมดข้างต้นที่นี่ (245 ดาวน์โหลด)



ดูพื้นที่นี้ฉันจะให้การปรับปรุงนี้มีการค้นพบใหม่ ...


ปล่อยให้ตอบ

NDK บ้าน | การแสดงไอที ​​| เพดานการแสดง | Penmenship การแสดง | Awe การแสดง | การแสดงตัวเอง