【问题标题】:Create anchoring with Xslt from xml document使用 Xslt 从 xml 文档创建锚定
【发布时间】:2014-07-20 19:30:53
【问题描述】:

我正在尝试使用 Xslt 1.0 从 xml 文档创建 html。 xml文档中有客户,部分客户的name属性中有“见XXXX”信息。

这里是xml文档;

<?xml version="1.0" encoding="UTF-8"?>
<customers>
    <customer name="Adam Dev" category="A1" phone="1234543" />
    <customer name="Jerry Sngiler (see Matt Pcneiv)" category="" phone="" />
    <customer name="Heid Schwan" category="B1" phone="121257456" />
    <customer name="Matt Pcneiv" category="A2" phone="65656565" />
    <customer name="John Lombak" category="A2" phone="787878787" />
    <customer name="April Ozil (see Heid Schwan)" category="" phone="" />
    <customer name="Terry Hill" category="B1" phone="1212121212" />    
</customers>

我想为xml文档中相关xml节点的“名称”属性创建锚定,如下所示。我的意思是当用户单击第二个节点上的Matt Pcneiv时,页面跳转到4上的Matt Pcneiv。 node.再次当用户点击 6.node 上的 Heid Schwan 时,页面跳转到 3.node 上的 Heid Schwan。

<html>
<head>
<style>
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
th,td
{
padding:5px;
}
</style>
</head>
<body>

<table style="width:600px">
<tr>
  <th>Name</th>
  <th>Category</th>     
  <th>Phone</th>
</tr>
<tr>
  <td>Adam Dev</td>
  <td>A1</td>       
  <td>1234543</td>
</tr>
<tr>
  **<td>Jerry Sngiler (see <a href="#C1">Matt Pcneiv</a>)</td>**
  <td></td>     
  <td></td>
</tr>
<tr>
  **<td><a id="C2">Heid Schwan</a></td>**
  <td>B1</td>       
  <td>121257456</td>
</tr>
<tr>
  **<td><a id="C1">Matt Pcneiv</a></td>**
  <td>A2</td>       
  <td>65656565</td>
</tr>
<tr>
  <td>John Lombak</td>
  <td>A2</td>       
  <td>787878787</td>
</tr>
<tr>
  **<td>April Ozil (see <a href="#C2">Heid Schwan</a>)</td>**
  <td></td>     
  <td></td>
</tr>
<tr>
  <td>Terry Hill</td>
  <td>B1</td>       
  <td>1212121212</td>
</tr>
</table>
</body>
</html>

我怎样才能用 xsl 做到这一点?如果你能帮助我,我将不胜感激。 谢谢。

【问题讨论】:

  • 您不想填写类别和电话并删除参考吗?
  • 我愿意,但这不是我可以做出的决定,谢谢。

标签: xml xslt anchor


【解决方案1】:

好的,那么。第一件事:设计输入 XML 的人是个白痴。一方面,您可以使用结构化数据工具;你为什么要把两个名字塞进一个槽里?另一方面,肯定有一天会有两个同名的客户。

除此之外,您需要在这里做的是提取引用的名称并将其用作链接到被引用人的锚点的键:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="customer-by-name" match="customer" use="@name" />

<xsl:template match="/">
    <table>
        <tr>
            <th>Name</th>
            <th>Category</th>     
            <th>Phone</th>
        </tr>
        <xsl:apply-templates select="customers/customer"/>  
    </table>
</xsl:template>

<xsl:template match="customer">
    <tr>
      <td><a id="{generate-id()}"><xsl:value-of select="@name"/></a></td>
      <td><xsl:value-of select="@category"/></td>       
      <td><xsl:value-of select="@phone"/></td>
    </tr>
</xsl:template>

<xsl:template match="customer[contains(@name, ' (see ')]">
    <xsl:variable name="ref-name" select="substring-before(substring-after(@name, ' (see '), ')')" />
    <tr>
        <td>
            <xsl:value-of select="substring-before(@name, ' (see ')"/>
            <xsl:text> (see </xsl:text>
            <a href="#{generate-id(key('customer-by-name', $ref-name))}"><xsl:value-of select="$ref-name"/></a>
            <xsl:text>)</xsl:text>
      </td>
      <td><xsl:value-of select="@category"/></td>       
      <td><xsl:value-of select="@phone"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 感谢迈克尔的解决方案。您认为可以在您的解决方案中添加类别节点上的分组吗?如何将 Muenchian 方法应用于您的解决方案?
  • 将两者结合起来应该不会有任何问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
相关资源
最近更新 更多