| for let where order by return | หลักการการทำงานคือ ใช้ในการผูกค่ากับตัวแปร โดยใช้คำสั่ง for หรือ let โดยค่าในที่นี้หมายถึง node ต่างๆ ในเอกสาร xml |
|---|---|
| Example 1 | ตัวอย่างการใช้ for in |
| Example 2 | ตัวอย่างการใช้ for in |
| Example 3 | ตัวอย่างการใช้ for in |
| Example 4 | ตัวอย่างการใช้ for in |
| Example 5 | ตัวอย่างการใช้ for in |
| Example 6 | ตัวอย่างการใช้ for in |
subject:for $value at $index in doc("filename.xml")xpath let $xpath := xpath where xpath = "ค่าข้อมูล" order by xpath ascending | descending return tagXml | xpath
content:หลักการการทำงานคือ ใช้ในการผูกค่ากับตัวแปร โดยใช้คำสั่ง for หรือ let โดยค่าในที่นี้หมายถึง node ต่างๆ ในเอกสาร xml ซึ่งผลลัพธ์ของการผูกค่าเรียกว่า tuple แปลว่า คู่ของตัวแปร กับ ค่าของตัวแปร โดย for ใช้กำหนดค่าให้ตัวแปรทีละค่าในลักษณะวนซ้ำ ( สามาถกำหนดให้เป็น loop หลายชั้นได้ โดยการ comma ดังตัวอย่างอันหนึ่ง ) โดย $value ใช้เก็บค่าที่ได้ และ $index ใช้เก็บตำแหน่งของค่านั้น โดย let ใช้กำหนดค่าทั้งหมดให้ตัวแปร โดยไม่มีการวนซ้ำ โดย where ใช้กำหนดเงื่อนไข โดย order by ใช้กำหนดการเรียงลำดับ โดย return ใช้กำหนดค่าที่จะคืนกลับ
example:<root>{ for $value at $index in doc("filename.xml")//website let $protocol := $value/domain/protocol let $name := $value/domain/name let $country := $value/country return <website position="{$index}" protocol="{string($protocol)}" country="{string($country)}">{string($name)}</website> }</root>
subject:ตัวอย่างการใช้ for in <?xml version="1.0" encoding="window-874" ?> <?xml-stylesheet type="text/xsl" href="filename.xsl" ?> <internet> <website id="1"> <domain> <protocol>http</protocol> <name>function</name> <extension>in.th</extension> </domain> <logo width="80px" /> <register>10 January 2553</register> <country>Thailand</country> </website> <website id="2"> <domain> <protocol>http</protocol> <name>catalogtoday</name> <extension>com</extension> </domain> <logo width="80px" /> <register>14 Febuary 2553</register> <country>Thailand</country> </website> </internet>
example:for $value in doc("filename.xml")//website where $value/domain/extension = "com" return $value/domain/name
subject:ตัวอย่างการใช้ for in <?xml version="1.0" encoding="window-874" ?> <?xml-stylesheet type="text/xsl" href="filename.xsl" ?> <internet> <website id="1"> <domain> <protocol>http</protocol> <name>function</name> <extension>in.th</extension> </domain> <logo width="80px" /> <register>10 January 2553</register> <country>Thailand</country> </website> <website id="2"> <domain> <protocol>http</protocol> <name>catalogtoday</name> <extension>com</extension> </domain> <logo width="80px" /> <register>14 Febuary 2553</register> <country>Thailand</country> </website> </internet>
example:for $value in doc("filename.xml")//website where contains($value/domain/extension/text(), "in") return $value/domain/name
subject:ตัวอย่างการใช้ for in <?xml version="1.0" encoding="window-874" ?> <?xml-stylesheet type="text/xsl" href="filename.xsl" ?> <internet> <website id="1"> <domain> <protocol>http</protocol> <name>function</name> <extension>in.th</extension> </domain> <logo width="80px" /> <register>10 January 2553</register> <country>Thailand</country> </website> <website id="2"> <domain> <protocol>http</protocol> <name>catalogtoday</name> <extension>com</extension> </domain> <logo width="80px" /> <register>14 Febuary 2553</register> <country>Thailand</country> </website> </internet>
example:for $value at $index in doc("filename.xml")//website let $protocol := $value/domain/protocol let $name := $value/domain/name let $country := $value/country order by $country ascending, $name descending return <website position="{$index}" protocol="{string($protocol)}" country="{string($country)}">{string($name)}</website>
subject:ตัวอย่างการใช้ for in <?xml version="1.0" encoding="window-874" ?> <?xml-stylesheet type="text/xsl" href="filename.xsl" ?> <internet> <website id="1"> <domain> <protocol>http</protocol> <name>function</name> <extension>in.th</extension> </domain> <logo width="80px" /> <register>10 January 2553</register> <country>Thailand</country> </website> <website id="2"> <domain> <protocol>http</protocol> <name>catalogtoday</name> <extension>com</extension> </domain> <logo width="80px" /> <register>14 Febuary 2553</register> <country>Thailand</country> </website> </internet>
example:for $value at $index in distinct-values(doc("filename.xml")//website/domain/extension) return <extension position="{$index}">{string($value)}</extension>
subject:ตัวอย่างการใช้ for in <?xml version="1.0" encoding="window-874" ?> <?xml-stylesheet type="text/xsl" href="filename.xsl" ?> <internet> <website id="1"> <domain> <protocol>http</protocol> <name>function</name> <extension>in.th</extension> </domain> <logo width="80px" /> <register>10 January 2553</register> <country>Thailand</country> </website> <website id="2"> <domain> <protocol>http</protocol> <name>catalogtoday</name> <extension>com</extension> </domain> <logo width="80px" /> <register>14 Febuary 2553</register> <country>Thailand</country> </website> </internet>
example:let $root := doc("filename.xml")//internet for $extension in distinct-values($root/website/domain/extension), $name in distinct-values($root/website/domain/name) order by $extension, $name return <website extension="{string($extension)}">{string($name)}</website>
subject:ตัวอย่างการใช้ for in <?xml version="1.0" encoding="window-874" ?> <?xml-stylesheet type="text/xsl" href="filename.xsl" ?> <internet> <website id="1"> <domain> <protocol>http</protocol> <name>function</name> <extension>in.th</extension> </domain> <logo width="80px" /> <register>10 January 2553</register> <country>Thailand</country> </website> <website id="2"> <domain> <protocol>http</protocol> <name>catalogtoday</name> <extension>com</extension> </domain> <logo width="80px" /> <register>14 Febuary 2553</register> <country>Thailand</country> </website> </internet>
example:for $value in doc("filename.xml")//website where contains($value/domain/extension/text(), "in") return <website name="{string($value/domain/name)}"> { for $subValue in $value where ! contains($value/domain/extension/text(), "in") return $subValue/domain/protocol/text() } </website>