【问题标题】:XSLT Replace Id, Get Value of other nodeXSLT 替换 Id,获取其他节点的值
【发布时间】:2017-11-06 09:29:49
【问题描述】:

大家好,我是 xml/xsl 的新手。我有以下 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Miau.xsl"?>
<export xmlns="urn:VocabularyExport">

<categories>
    <category name="A" id="123" />
    <category name="B" id="456" />
</categories>

<vocables>
    <vocable categoryId="123" spanishWord="uno" engWord="one" id="45d0a344-785b-
4463-9877-5474c99fdc74" />
    <vocable categoryId="456" spanishWord="dos" engWord="two" id="03efffbb-1cbf-
44f9-a377-74da252daac0" />
</vocables>
</export>

为了在表格中显示它,我有以下(工作)XSL 代码:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:l="urn:VocabularyExport">

<xsl:key name="category" match="category" use="@id" />
<xsl:template match="/">
<html>
   <body>

    <h1>My Vocable Colletion</h1>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Id</th>
        <th>Deutsches Wort</th>
        <th>Spanishes Wort</th>
        <th>CategoryId</th>
      </tr>
      <xsl:for-each select="l:export/l:vocables/l:vocable">
        <tr>
          <td>
            <xsl:value-of select="@id"/>
          </td>
          <td>
            <xsl:value-of select="@germanWord"/>
          </td>
          <td>
            <xsl:value-of select="@spanishWord"/>
          </td>
          <td>
            <xsl:value-of select="@categoryId"/>
          </td>
        </tr>
       </xsl:for-each>
      </table>
     </body>
    </html>
  </xsl:template>
 </xsl:stylesheet>

我想用类别名称代替categoryId(参见节点categories)。有什么建议吗?

【问题讨论】:

    标签: xml xslt replace


    【解决方案1】:

    您已经为 XML/XSLT 的新手做了一个良好的开端,正如您在处理 XSLT 中的名称空间时添加的那样。但是,您在密钥中错过了这一点。应该是这个

    <xsl:key name="category" match="l:category" use="@id" />
    

    然后,要获取类别名称,你想要的表达式是这样的:

    <xsl:value-of select="key('category', @categoryId)/@name"/>
    

    【讨论】:

    • 哦,该死的我已经尝试了几乎相同的方法,但我的“匹配”是错误的:D 非常感谢你,伙计;)
    猜你喜欢
    • 1970-01-01
    • 2011-03-13
    • 2016-04-30
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    相关资源
    最近更新 更多