使用XSL更改输出XML值

时间:2013-10-07 10:46:20

标签: xml xslt xpath xslt-2.0

我希望我的输出XML具有不同的值。我有一张桌子。

让我们说有很多学生..

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<Person>
        <lookuptable>
          <name first="Jen" ori="Jenny" />
          <name first="Sam" ori="Sammy" />
        </lookuptable>
    <Student>
        <Info Name="Jen" Age="20" Class="C" />
    </Student>
    <Student>
        <Info Name="Sam" Age="21" Class="B" />

    </Student>

</Person>

必需的输出

<?xml version="1.0" encoding="UTF-8"?>
<Person>
        <lookuptable>
          <name first="Jen" ori="Jenny" />
          <name first="Sam" ori="Sammy" />
        </lookuptable>
    <Student>
        <Info Name="Jenny" Age="20" Class="C" />
    </Student>
    <Student>
        <Info Name="Sammy" Age="21" Class="B" />

    </Student>

</Person>

如何从查找表中获取Jenny,Sammy等?意味着每个地方都出现Jen,它应该使用表格中的Jenny。我不知道怎么写XSL。

1 个答案:

答案 0 :(得分:1)

定义密钥,使用身份转换模板以及该属性的模板:

<xsl:key name="k1" match="lookuptable/name" use="@first"/>


<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>


<xsl:template match="Student/Info/@Name">
  <xsl:attribute name="{name()}" select="key('k1', .)/@ori"/>
</xsl:template>
相关问题