คำถาม XSLT แบบง่าย: การตั้งค่าแอตทริบิวต์ของโหนด HTML ภายใน เช่น ‹div id=something›

ฉันมีช่วงเวลาที่ยากลำบากในการหาคำตอบสำหรับคำถามต่อไปนี้ ซึ่งดูเหมือนจะเป็นเรื่องธรรมดา ดังนั้นฉันจึงขาดสิ่งพื้นฐานบางอย่างไป คุณช่วยฉันหน่อยได้ไหม?

ด้วยสคีมา XML ที่วางแผนไว้ อินพุต XML ตัวอย่าง และ XSLT ตัวอย่างด้านล่างที่ใช้ในการแปลง XML เป็น HTML ฉันจะตั้งค่าคุณสมบัติภายในแท็กได้อย่างไร เช่น <div id=HouseNumber>,<input type="checkbox" id=Zipcode> ฯลฯ?

หมายเหตุ: การไม่มีเครื่องหมายคำพูดรอบ HouseNumber และรหัสไปรษณีย์นั้นมีจุดประสงค์ ฉันกำลังพยายามใส่ค่าของคุณลักษณะเหล่านี้จากอินพุต XML ลงใน id="", for="", name="" ฯลฯ

ขอขอบคุณที่สละเวลาและป้อนข้อมูลเกี่ยวกับคำถามเวอร์ชันแรก

bn

ตัวอย่างสคีมา XML

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema elementFormDefault="qualified"  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Location">
        <xs:complexType>
            <xs:attribute name="State" type="xs:string" use="required" />
            <xs:attribute name="County" type="xs:string" use="required" />
            <xs:attribute name="City" type="xs:string" use="required" />
            <xs:attribute name="Zipcode" type="xs:nonNegativeInteger" use="required" />
            <xs:attribute name="HouseNumber" type="xs:nonNegativeInteger" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>

ตัวอย่างอินพุต XML:

<Location>
    <State>California</State>
    <County>Los Angeles County</County>
    <City>Los Angeles</City>
    <Zipcode>90210</Zipcode>
    <HouseNumber>123</HouseNumber>
</Location>

ตัวอย่าง XSLT:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
    <xsl:for-each select="Location">
        <!--Inner HTML example, div has no id-->
        <div class="houseStyle">
            <ul>
                <li><xsl:value-of select="Location/State"/></li>
                <li><xsl:value-of select="Location/County"/></li>
                <li><xsl:value-of select="Location/City"/></li>
                <li><xsl:value-of select="Location/Zipcode"/></li>
            </ul>
        </div>
        <!--Inner HTML example again, but how do I
            set the div id to HouseNumber?-->
        <div class="houseStyle" id=HouseNumber>
            <ul>
                <li><xsl:value-of select="Location/State"/></li>
                <li><xsl:value-of select="Location/County"/></li>
                <li><xsl:value-of select="Location/City"/></li>
                <li><xsl:value-of select="Location/Zipcode"/></li>
            </ul>
        </div>
    </xsl:for-each>
</xsl:stylesheet>

เอาต์พุต HTML ที่ต้องการ โดยที่แท็ก div มีรหัสของบ้านเลขที่:

<div class="houseStyle" id="123">
    <ul>
        <li>California</li>
        <li>Los Angeles County</li>
        <li>Los Angeles</li>
        <li>90210</li>
    </ul>
</div>

person bn.    schedule 06.10.2009    source แหล่งที่มา
comment
จะมีประโยชน์มากหากคุณสามารถโพสต์ตัวอย่างอินพุต XML และสิ่งที่คุณคาดหวังให้ผลลัพธ์ออกมาได้   -  person Jim Garrison    schedule 06.10.2009
comment
โทรหาจิม ฉันต้องโพสต์ตัวอย่างที่ถูกสุขลักษณะ แต่ฉันจะพยายามทำให้มันป้านน้อยลง   -  person bn.    schedule 06.10.2009


คำตอบ (3)


ไม่ชัดเจนว่าคุณต้องการอะไรที่นี่ คุณหมายความว่าคุณต้องการตั้งค่าแอตทริบิวต์ของคุณเป็นผลลัพธ์ของนิพจน์ XPath บางอย่าง (เช่น Delta) หรือไม่ หากเป็นเช่นนั้น สิ่งนี้ควรทำ:

<div class="stylishClass" id="{Delta}">

หรือคุณสามารถใช้ <xsl:element> และ <xsl:attribute> ตามที่คำตอบอื่น ๆ อธิบาย แม้ว่ากรณีการใช้งานโดยทั่วไปคือเมื่อต้องสร้างชื่อองค์ประกอบ/แอตทริบิวต์เอง

person Pavel Minaev    schedule 06.10.2009
comment
ขอบคุณมากมันทำงานได้ดีมาก ขอขอบคุณสำหรับแนวทางอื่นและเหตุผลที่อยู่เบื้องหลังพวกเขา - person bn.; 07.10.2009

พยายาม:

<xsl:element name="div">
        <xsl:attribute name="class">stylishClass</xsl:attribute>
        <xsl:attribute name="id"><xsl:value-of select="Delta"/></xsl:attribute>  
 </xsl:element>

ตัวอย่างแท็ก Anchor:

<xsl:element name="a">
      <xsl:attribute name="href">http://example.com</xsl:attribute>            
      <xsl:text>My Link Text</xsl:text>
</xsl:element>
person Mike Marshall    schedule 06.10.2009
comment
ขอบคุณสิ่งนี้ได้ผล คำตอบของคุณได้รับในเวลาเดียวกันกับของ Rubens Farias แต่คุณทั้งคู่ช่วยฉันด้วยขอบคุณ! - person bn.; 07.10.2009

แล้วโค้ดด้านล่างล่ะ?


    <xsl:element name="div">
        <xsl:attribute name="class">stylishClass</xsl:attribute>
        <xsl:attribute name="id"><xsl:value-of select="Delta"/></xsl:attribute>
    </xsl:element>

person Rubens Farias    schedule 06.10.2009
comment
ขอบคุณสิ่งนี้ได้ผล คำตอบของคุณได้รับในเวลาเดียวกันกับของ mjmarsh แต่คุณทั้งสองช่วยฉันออกไปขอบคุณ! - person bn.; 07.10.2009