使用 XSLT 从一个 XML 中提取值,同时匹配其他 XML 中的模板 [英] Extract value from one XML while matching template in other XML using XSLT

查看:13
本文介绍了使用 XSLT 从一个 XML 中提取值,同时匹配其他 XML 中的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 XML.我正在尝试在第一个 XML 上运行 XSLT,并使用键匹配第二个 XML 中的数据.在对第二个 XML 进行模板匹配时,我无法从第一个 XML 的匹配节点中提取数据(不知道如何从那里提取任何数据)并将其填充到那里.以下是示例和预期输出.

I have 2 XMLs. I am trying to run XSLT on 1st XML and matching data in 2nd XML using keys. While doing template-match on 2nd XML,I am unable to pull data from 1st XML's matching node(dont know how to pull any data from there per say) and populate it in there. Below are samples and expected output.

第一个 XML-

<parent>
    <child>
        <name>John</name>
        <city>Boston</city>
        <shortCityCode>B</shortCityCode>
    </child>
    <child>
        <name>John</name>
        <city>Seattle</city>
        <shortCityCode>S</shortCityCode>
    </child>
    <child>
        <name>Allison</name>
        <city>Houston</city>
        <shortCityCode>H</shortCityCode>
    </child>
</parent>

第二个 XML - 作为变量内联在 XSLT 中

2nd XML - inline in the XSLT as variable

XSLT 尝试-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:param name="details">
<details>
    <parent>
        <detail>
            <city>Boston</city>
            <code>abc</code>
        </detail>
        <detail>
            <city>Houston</city>
            <code>xyz</code>
        </detail>
    </parent>
    <parent>
        <detail>
            <city>Boston</city>
            <code>abc</code>
        </detail>
        <detail>
            <city>Seattle</city>
            <code>mno</code>
        </detail>
    </parent>
    <parent>
        <detail>
            <city>Houston</city>
            <code>xyz</code>
        </detail>
        <detail>
            <city>Seattle</city>
            <code>mno</code>
        </detail>
    </parent>
</details>      
  </xsl:param>

  <xsl:key name="parent-ref" match="parent" use="detail/city"/>
  <xsl:key name="detail-ref" match="parent/detail" use="city"/>

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="parent">
    <FinalData>
        <xsl:for-each-group select="child" group-by="name">
            <Data>
                <xsl:copy-of select="name"/>
            </Data>
            <Details>
                <xsl:apply-templates select="key('parent-ref', current-group()/city, $details)"/>
            </Details>
        </xsl:for-each-group>
    </FinalData>
  </xsl:template>

  <xsl:template match="details/parent">
      <detail>
          <xsl:apply-templates select="key('detail-ref', current-group()/city, .)"/>
      </detail>
  </xsl:template>

  <xsl:template match="detail">
      <city value="{shortCityCode}"> //Here I want to populate the value from 1st XML
          <xsl:value-of select="code"/>
      </city>
  </xsl:template>
</xsl:stylesheet>

我需要提取 shortCityCode 字段并填充到上述 XSLT 中的第二个模板调用中.下面是上面需要的 XSLT 代码片段 -

I need to extract the shortCityCode field and populate in 2nd Template call in the above XSLT. Below is the Snippet of above XSLT where its needed -

  <xsl:template match="detail">
      <city value="{shortCityCode}"> //shortCityCode from 1st XML where key is matching the values.
          <xsl:value-of select="code"/>
      </city>
  </xsl:template>

预期输出 -

<FinalData>
    <Data>
        <name>John</name>
        <details>
            <detail>
                <city value="B">abc</city>
            </detail>
            <detail>
                <city value="B">abc</city>
                <city value="S">mno</city>
            </detail>
            <detail>
                <city value="S">mno</city>
            </detail>
        </details>
    </Data>
    <Data>
        <name>Allison</name>
        <details>
            <detail>
                <city value="H">xyz</city>
            </detail>
            <detail>
                <city value="H">xyz</city>
            </detail>
        </details>
    </Data>
</FinalData>

推荐答案

我觉得你需要把模板改成

I think you need to change the template to

  <xsl:template match="detail">
      <city value="{current-group()[city = current()/city]/shortCityCode}">
          <xsl:value-of select="code"/>
      </city>
  </xsl:template>

https://xsltfiddle.liberty-development.net/gVhDDyY/5

这篇关于使用 XSLT 从一个 XML 中提取值,同时匹配其他 XML 中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆