iT邦幫忙

0

xslt如何取得xml tag屬性

  • 分享至 

  • xImage

在php中對xml使用setAttribute,想請問大大們要怎麼在xslt中取得option,value,name,valueString,type這些屬性的值,才能讓xsl:if判斷?
ex:想利用option的屬性判斷Y為標題,N為對應的值。
https://ithelp.ithome.com.tw/upload/images/20211201/20120931ctiL1HEMuW.png

<text>
            <text1 option="Y" value="" name="" valueString="" type=""><![CDATA[標題1]]></text1>
            <text1 option="N" value="" name="" valueString="" type=""><![CDATA[標題1的值]]></text1>
</text>
<xsl:for-each select="text/text1">
<xsl:if >

 </xsl:if>
</xsl:for-each>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
海綿寶寶
iT邦大神 1 級 ‧ 2021-12-01 11:05:29
最佳解答

https://ithelp.ithome.com.tw/upload/images/20211201/20001787ywqHKo6CAA.png

<?xml version="1.0" encoding="UTF-8"?>
<text>
            <text1 option="Y" value="v" name="n" valueString="vs" type="t"><![CDATA[標題1]]></text1>
            <text1 option="N" value="" name="" valueString="" type=""><![CDATA[標題1的值]]></text1>
</text>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>XSLT sample</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Value</th>
        <th>Option</th>
        <th>value</th>
        <th>name</th>
        <th>valueString</th>
        <th>type</th>
      </tr>
      <xsl:for-each select="text">
      <tr>
        <td><xsl:value-of select="text1" /></td>
        <td><xsl:value-of select="text1/@option" /></td>
        <td><xsl:value-of select="text1/@value" /></td>
        <td><xsl:value-of select="text1/@name" /></td>
        <td><xsl:value-of select="text1/@valueString" /></td>
        <td><xsl:value-of select="text1/@type" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

可以到W3S xslt即時線上修改測試

看更多先前的回應...收起先前的回應...
qpalzm iT邦新手 1 級 ‧ 2021-12-01 11:24:50 檢舉

謝謝海綿大大/images/emoticon/emoticon41.gif

qpalzm iT邦新手 1 級 ‧ 2021-12-01 11:52:29 檢舉

想在請問xsl:when只能判斷數值>或<嗎?可以判斷字串嗎,如下:
<xsl:choose> <xsl:when test="text1@code=F"> <td>性別:女</td> </xsl:when> <xsl:otherwise> <td>性別:男</td> </xsl:otherwise> </xsl:choose>
目前測試都只能做到數值的判斷 ,是因為沒做跳脫字元的動作嗎?懇請解惑

參考這篇
簡單說:可以判斷字串,用單引號,大概像這樣
<xsl:when test="text1@code='F'">

qpalzm iT邦新手 1 級 ‧ 2021-12-01 13:45:52 檢舉

謝謝海綿大大~我嘗試用跳脫字元後發現也可以正常持行,貼上來分享一 下

<xsl:when test="text1@code=&apos;F&apos;">

我要發表回答

立即登入回答