xslt - 将字符串值转换为 XML 标记名称

标签 xslt

以下是我的要求。我们可以使用 XSLT 做到这一点吗?我想将 AttributeName 的值转换为策略下的标记,并将相应的 AttributeValue 转换为值。

输入:

<Policy>
    <Attributes>
        <AttributeName>is_policy_loan</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>
    <Attributes>
        <AttributeName>is_policy_owners</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>       
    <Attributes>
        <AttributeName>is_policy_twoyears</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>       
</Policy>

输出:

<Policy>
    <is_policy_loan>Yes</is_policy_loan>
    <is_policy_owners>Yes</is_policy_owners>
    <is_policy_twoyears>Yes</is_policy_twoyears>
</Policy>

最佳答案

下面的 xsl 文件将完成这项工作:

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

  <!-- create the <AttributeName>AttributeValue</..> nodes -->
  <xsl:template match="//Attributes">
    <xsl:variable name="name" select="AttributeName" />
    <xsl:element name="{$name}">
      <xsl:value-of select="AttributeValue" />
    </xsl:element>
  </xsl:template>

  <!-- wrap nodes in a `Policy` node -->
  <xsl:template match="/">
    <Policy>
      <xsl:apply-templates/>
    </Policy>
  </xsl:template>
</xsl:stylesheet>

关于xslt - 将字符串值转换为 XML 标记名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776971/

相关文章:

xml - DocBook-XML 和网络帮助

java - 无法让 setParameter 在 XSLT 转换器中工作

java - XSLT:我们可以使用 abs 值吗?

html - 简单的 XML 到 HTML 表格

mysql - 使用 XPATH fn :concat in MySQL ExtractValue does not process more than two arguments

java - 如何根据 xslt 中的特定值检查所有列表节点字段?

java - 解析 XML 文件获取所有 Namespace 信息

xml - 复制某些属性存在(或不存在)的节点

xml - 如何正确使用xsl :group-start-with with following siblings

xml - 使用 xpath 查找父节点的位置