Xslt中属性的访问方法总结

来源:互联网 发布:cisco 清除端口配置 编辑:程序博客网 时间:2024/05/24 02:43
访问属性的方法和访问元素的方法是一样的。注意属性名前面有个"@"符号

XML源码
<source>

<dog name="Joe">
     <data weight="18 kg" color="black"/>
</dog>

</source>

输出
<p>
  <b>Dog: </b>Joe</p>
<p>
  <b>Color: </b>black</p>

用HTML察看

Dog: Joe

Color: black

XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="dog">
     <p>
          <b>
               <xsl:text>Dog: </xsl:text>
          </b>
          <xsl:value-of select="@name"/>
     </p>
     <p>
          <b>
               <xsl:text>Color: </xsl:text>
          </b>
          <xsl:value-of select="data/@color"/>
     </p>
</xsl:template>


</xsl:stylesheet>


属性和元素的处理方法是一样的。

XSLT stylesheet 1

XML源码
<source>

<employee id="js0034"> Joe Smith </employee>

</source>

输出
Joe Smith
[<b>
  <i>js0034</i>
</b>]

用HTML察看
Joe Smith [ js0034 ]
XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="employee">
     <xsl:value-of select="."/>
     <xsl:text>[</xsl:text>
     <xsl:apply-templates select="@id"/>
     <xsl:text>]</xsl:text>
</xsl:template>

<xsl:template match="@id">
     <b>
          <i>
               <xsl:value-of select="."/>
          </i>
     </b>
</xsl:template>


</xsl:stylesheet>

你也可以通过是否包含某些属性值来选择元素。 XSLT stylesheet 1 选择了,而 XSLT stylesheet 2 排除了那些包含特定属性的元素。

XSLT stylesheet 1

XML源码
<source>

<car id="a234" checked="yes"/>
<car id="a111" checked="yes"/>
<car id="a005"/>

</source>

输出
<p>Car: a234</p>

<p>Car: a111</p>

用HTML察看

Car: a234

Car: a111

XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="car[@checked]">
     <p>
          <xsl:text>Car: </xsl:text>
          <xsl:value-of select="@id"/>
     </p>
</xsl:template>


</xsl:stylesheet>



XSLT stylesheet 2

XML源码
<source>

<car id="a234" checked="yes"/>
<car id="a111" checked="yes"/>
<car id="a005"/>

</source>

输出
<p>Car: a005</p>

用HTML察看

Car: a005

XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="car[not(@checked)]">
     <p>
          <xsl:text>Car: </xsl:text>
          <xsl:value-of select="@id"/>
     </p>
</xsl:template>


</xsl:stylesheet>


 
原创粉丝点击