Extract value from one XML while matching template in other XML using XSLT

Deadly 提交于 2020-06-17 13:22:08

问题


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.

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

2nd XML - inline in the XSLT as variable

XSLT Attempt-

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

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>

Expected Output -

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

回答1:


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



来源:https://stackoverflow.com/questions/62195646/extract-value-from-one-xml-while-matching-template-in-other-xml-using-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!