使用XSL将XML转换为另一种XML

时间:2013-10-01 03:30:38

标签: xml xslt xpath xslt-2.0

我还在学习XSL,我正在尝试将我从XSL获得的值更改为另一个值。 (我正在使用在线网站将我的XML转换为使用XSL的另一种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>

我创建了一个查找表

输出

<?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>

我想改变我的输出,这意味着每次“Jen”出现我希望它是“Jenny”等使用我的查找表。我怎么能实现这个目标?或者更容易创建另一个XSL来将输出(最后的XML)转换为我需要的要求?提前谢谢..

2 个答案:

答案 0 :(得分: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>

有关详细信息,请参阅Pavel Minaev对XSL: How best to store a node in a variable and then us it in future xpath expressions?的回答 它还解释了在使用XSLT 2时不需要exsl扩展。因此,您可能希望改进我的XSLT 2方法。

答案 1 :(得分:1)

这是@ halfbit答案的XSLT 2.0版本(我没有将查找表复制到输出中,我认为不是真的需要):

<?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>
相关问题