質問

私は次の質問に対する答えを見つけるのに苦労しています。助けてくれませんか?

不自然なXMLスキーマ、サンプルXML入力、および以下のサンプルXSLTがXMLをHTMLに変換するために使用されることを考えると、タグ内で属性を設定するにはどうすればよいですか?例えば <div id=HouseNumber>,<input type="checkbox" id=Zipcode>, 、など?

ノート: HouseNumberとZipcodeの周りの引用の欠如は純粋です。これらの属性の値を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タグには家番号のIDがあります。

<div class="houseStyle" id="123">
    <ul>
        <li>California</li>
        <li>Los Angeles County</li>
        <li>Los Angeles</li>
        <li>90210</li>
    </ul>
</div>
役に立ちましたか?

解決

ここで何が欲しいかは明らかではありません。 XPath式の結果に属性を設定したいということですか( Delta)?もしそうなら、これはトリックをするはずです:

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

または、使用することもできます <xsl:element><xsl:attribute>, 、他の答えが説明するように、そのための典型的なユースケースは、要素/属性名自体を生成する必要がある場合です。

他のヒント

試す:

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

アンカータグの例:

<xsl:element name="a">
      <xsl:attribute name="href">http://example.com</xsl:attribute>            
      <xsl:text>My Link Text</xsl:text>
</xsl:element>

以下のコードはどうですか?


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

Hth

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top