使用 XSLT 中的另一个变量值查找属性



我需要能够转换这个XML:

<Root>
  <Fields>
    <Field ID="XYZ" Value="M" />
    <Field ID="XYZ.DECODED" Value="Male" />    
    <Field ID="ABC.DECODED" Value="Yellow" />
    <Field ID="ABC" Value="Y" />
    <Field ID="123.DECODED" Value="Low" />
    <Field ID="456" Value="Smith" />
    <Field ID="123" Value="1" />    
  </Fields>
</Root>

到此 XML 中:

<Root>
  <Fields>
    <Field ID="XYZ" Value="M" DisplayValue="Male" />
    <Field ID="ABC" Value="Y" DisplayValue="Yellow" />
    <Field ID="456" Value="Smith" DisplayValue="Smith" />
    <Field ID="123" Value="1" DisplayValue="Low" />
  </Fields>
</Root>

使用 XSLT。 "XYZ"、"ABC"、"123"等 ID 属性我不会提前知道。 有什么想法吗? 是否需要从变量创建 XPATH 表达式?

这能满足你的要求

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Field">
        <xsl:variable name="ref" select="concat(@ID,'.DECODED')"/>
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:attribute name="DisplayValue"><xsl:value-of select="//Field[@ID=$ref]/@Value"/></xsl:attribute>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Field[contains(@ID,'.DECODED')]"/>
</xsl:stylesheet>

它是一个标识转换加上两个模板,一个用于消除在 ID 属性中具有.DECODED的 Field 节点,另一个用于复制所需的节点并添加值。

我添加了一些内容来为没有 .解码值。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Field">
    <xsl:variable name="ref" select="concat(@ID,'.DECODED')"/>
    <xsl:copy>
      <xsl:variable name="dv">
        <xsl:choose>
          <xsl:when test="//Field[@ItemOID=$ref]/@Value">
            <xsl:value-of select="//Field[@ItemOID=$ref]/@Value"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@Value"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="DisplayValue">
        <xsl:value-of select="$dv"/>
      </xsl:attribute>
    </xsl:copy>    
  </xsl:template>
  <xsl:template match="Field[contains(@ID,'.DECODED')]"/>
</xsl:stylesheet>

最新更新