【问题标题】:Find attribute using another variable value in XSLT在 XSLT 中使用另一个变量值查找属性
【发布时间】:2017-02-03 02:48:45
【问题描述】:

我需要能够转换此 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 xslt xpath xslt-1.0 xslt-2.0


    【解决方案1】:

    这就是你想要的

    <?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节点,一个用于复制您想要的节点并添加值。

    【讨论】:

    • 完美!谢谢!
    【解决方案2】:

    我添加了一点来为没有 .DECODED 值的元素添加 DisplayValue。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多