2013-10-01 62 views
1

我仍在学习XSL,并试图将我从XSL获得的值更改为另一个值。 (我使用的是在线网站我的XML转换为另一种XML使用XSL)使用XSL将XML转换为另一个XML

XML

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

    </Student> 

</Person> 

可以说有很多students(甚至相同的名字)

XSLT

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

    <xsl:template match="Person"> 
     <Person> 

     <lookuptable> 
      <name first="Jen" ori="Jenny" /> 
      <name first="Sam" ori="Sammy" /> 
     </lookuptable> 

     <xsl:for-each select="Student"> 
      <Info Name="{Info/@Name}" Age="{Info/@Age}" Class="{Info/@Class}" /> 
     </xsl:for-each> 

     </Person> 
    </xsl:template> 
</xsl:stylesheet> 

我创建了一个LookupTable中

输出

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

我想改变我的输出,每一个“仁”看来我希望它是“珍妮”等用我LookupTable中时间的意义。我怎么能做到这一点?或者是否更容易创建另一个XSL来将输出(最后一个XML)转换为我需要的要求?在此先感谢..

回答

1

这里是XSLT 2.0版本@ halfbit的回答(我没有复制LookupTable中的输出,我认为是不是真的想):

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

    <xsl:variable name="lookuptable" as="element(name)*"> 
     <name first="Jen" ori="Jenny" /> 
     <name first="Sam" ori="Sammy" /> 
    </xsl:variable> 

    <xsl:template match="Person"> 
     <Person> 
     <xsl:for-each select="Student"> 
      <xsl:variable name="key" select="Info/@Name"/> 
      <Info Age="{Info/@Age}" Class="{Info/@Class}"> 
      <xsl:attribute name="Name" select="$lookuptable[@first=$key]/@ori"/> 
      </Info> 
     </xsl:for-each> 
     </Person> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感谢... – Hash

1

您可以将查找表放入一个变量并通过节点集扩展使用它。我试了一下http://www.xsltcake.com/

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
    xmlns:exsl="http://exslt.org/common"> 

    <xsl:variable name="lookuptable"> 
    <lookuptable> 
     <name first="Jen" ori="Jenny" /> 
     <name first="Sam" ori="Sammy" /> 
    </lookuptable> 
    </xsl:variable> 

    <xsl:template match="Person"> 
     <Person> 

     <xsl:copy-of select="$lookuptable"/> 

     <xsl:for-each select="Student"> 
      <xsl:variable name="key" select="Info/@Name"/> 
      <Info Age="{Info/@Age}" Class="{Info/@Class}"> 
      <xsl:attribute name="Name"> 
       <xsl:value-of select="exsl:node-set($lookuptable)/lookuptable/name[@first=$key]/@ori"/> 
      </xsl:attribute> 
      </Info> 
     </xsl:for-each> 

     </Person> 
    </xsl:template> 
</xsl:stylesheet> 

有关此看到帕维尔Minaev的回答细节XSL: How best to store a node in a variable and then us it in future xpath expressions? 这也解释了使用XSLT 2.所以,你可能想改进我的方法对XSLT 2,当你将不需要exsl扩展。