【问题标题】:Change output XML values using XSL使用 XSL 更改输出 XML 值
【发布时间】:2013-10-07 10:46:19
【问题描述】:

我希望我的输出 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。

【问题讨论】:

    标签: xml xslt xpath xslt-2.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>
    

    【讨论】:

    • 不应该是&lt;xsl:apply-templates select="@* | node()"/&gt;吗?
    • @markdark,使用 XSLT 2.0,您可以编写 &lt;xsl:apply-templates select="@* , node()"/&gt;&lt;xsl:attribute name="{name()}" select="key('k1', .)/@ori"/&gt;。由于问题被标记为xslt-2.0,因此我使用了该语言版本的功能。
    • 如果我想将年龄值更改为(例如:-20)(假设还有另一行用于查找具有适当值的表)。像上面的名称,我如何将其添加到 xsl 中跨度>
    • 定义另一个键&lt;xsl:key name="k2" match="numbers/number" use="@value"/&gt;(假设你有例如&lt;numbers&gt;&lt;number value="20"&gt;twenty&lt;/number&gt;&lt;/numbers&gt;)然后添加一个模板&lt;xsl:template match="Student/Info/@Age"&gt;&lt;xsl:attribute name="{name()}" select="key('k2', .)"/&gt;&lt;/xsl:template&gt;
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多