Xml 如何在xslt中为每个节点选择相同的节点

Xml 如何在xslt中为每个节点选择相同的节点,xml,xslt,Xml,Xslt,我有以下xml <phones> <phone> <code>+37529</code> <number>8546632</number> </phone> <phone> <code>+37517</code> <number>4562389</num

我有以下xml

    <phones>
      <phone>
        <code>+37529</code>
        <number>8546632</number>
      </phone>
      <phone>
        <code>+37517</code>
        <number>4562389</number>
      </phone>
   </phones>
我需要得到下面的html表

在此之前,我有下一个html表

所以我的xsl文件就像

<table>
...
   <xsl:for-each select="phones/phone">
      <tr>
         <td width="25%">Code</td>
         <td width="25%">
            <xsl:value-of select="code" />
            &#160;
         </td>
         <td width="25%">Number</td>
         <td width="25%">
            <xsl:value-of select="number" />
            &#160;
         </td>
      </tr>
   </xsl:for-each>
...
</table>

...
密码
 
数字
 
...
现在我应该划分电话,我不知道如何使用旧的xml文件进行xslt转换。请帮助我构建正确的xsl文件。谢谢。

我“为每个节点选择节点”的方法是使用模板。例如:

XML 给定以下XML(根据您的评论,我冒昧添加了
@type
):

XSL 以及以下XSL模板:

<xsl:template match="//phones">
    <table><xsl:apply-templates /></table>
</xsl:template>

<xsl:template match="phone">
    <tr>
        <th colspan='4'><xsl:value-of select="@type" />:</th>
    </tr>
    <tr>
        <th>Code</th><td><xsl:value-of select="code" /></td>
        <th>Number</th><td><xsl:value-of select="number" /></td>
    </tr>
</xsl:template>

:
密码
数字
提供所需的html表输出例如


如果要设置表格的宽度(样式),我建议使用CSS而不是
width=“25%”
例如

总是有两个电话号码吗?第二个总是手机吗?实际上最大电话号码可以是3:办公室、手机和家庭。其中一个可能会被遗漏,或者两个,或者全部被遗漏。那么你怎么知道哪一个是哪一个呢?是的,你是对的。如果一个电话丢失了,我不知道哪个是哪个。因此,我认为我的最佳解决方案是更改我的xml。实际上,您将需要类似于
等的内容。
<xsl:template match="//phones">
    <table><xsl:apply-templates /></table>
</xsl:template>

<xsl:template match="phone">
    <tr>
        <th colspan='4'><xsl:value-of select="@type" />:</th>
    </tr>
    <tr>
        <th>Code</th><td><xsl:value-of select="code" /></td>
        <th>Number</th><td><xsl:value-of select="number" /></td>
    </tr>
</xsl:template>