Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

java - How to map value from xml to XSLT

I am trying to populate data from xml to xslt n unable to do that since I do not have idea about xslt and xpath.Even though I tried my level best ( used concat() method ) but could not. could you please help me with below mapping? :----

Xml:----(a piece of code)

<supportingProductFeatures>
    <type>NH</type>
    <version>2.0.0</version>
    <capacityAvailability>
      <featureType>TY1</featureType>
      <capacity>2</capacity>
      <unitOfMeasure>Mbps</unitOfMeasure>
      <highSpeedNotLessThan>true</highSpeedNotLessThan>
    </capacityAvailability>
  </supportingProductFeatures>

trying to achieve below one:--

<DescribedBy>
  <value>Yes</value> 
  <Characteristic>
    <name>NH TY1 High Speed Tiers (greater or equal to 2Mbps)</name> 
    <type>abcd</type>
  </Characteristic>
</DescribedBy>

condition to map:---

         if ((highSpeedNotLessThan!=null))
    {
            if(highSpeedNotLessThan.equals("true"){
            
            1) set value=yes
            2) set name=concat(type +" "+featureType+" "+"High Speed Tiers (greater or equal to 
              "+capacity+unitOfMeasure+")"
            3)  set type="abcd"
      }
            else if(highSpeedNotLessThan.equals("false"){
            
            1)set value=no
            2) set name=concat(type +" "+featureType+" "+"High Speed Tiers (greater or equal to 
              "+capacity+unitOfMeasure+")"
            3)  set type="abcd"
            }
}

this is what i have tried so far:--

<DescribedBy>
<xsl:if test="/supportingProductFeatures/capacityAvailability/highSpeedNotLessThan='true'">
<value>yes</value>
</xsl:if>
<xsl:if test="/supportingProductFeatures/capacityAvailability/highSpeedNotLessThan='false'">
<value>No</value>
</xsl:if>
<Characteristic>
    <name>
<xsl:value-of select="concat(supportingProductFeatures/type,' ',supportingProductFeatures/capacityAvailability/featureType,' ','High Speed Tiers (greater or equal to ',supportingProductFeatures/capacityAvailability/capacity,supportingProductFeatures/capacityAvailability/unitOfMeasure)" />
</name> 
    <type>abcd</type>
  </Characteristic>

 </DescribedBy>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's how you could do it using XSLT.

<?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="/">
    <Output>
      <xsl:apply-templates select="//supportingProductFeatures"/>
    </Output>
  </xsl:template>
  
  <xsl:template match="supportingProductFeatures">
    <DescribedBy>
      <xsl:choose>
        <xsl:when test="capacityAvailability/highSpeedNotLessThan='true'">
          <value>Yes</value>
          <Characteristic>
            <name><xsl:value-of select="concat(type,' ',capacityAvailability/featureType,' ','High Speed Tiers (greater or equal to ',capacityAvailability/capacity,capacityAvailability/unitOfMeasure,')')"/></name>
          </Characteristic>
          <Type>abcd</Type>
        </xsl:when>
        <xsl:when test="capacityAvailability/highSpeedNotLessThan='false'">
          <value>No</value>
          <Characteristic>
            <name><xsl:value-of select="concat(type,' ',capacityAvailability/featureType,' ','High Speed Tiers (greater or equal to ',capacityAvailability/capacity,capacityAvailability/unitOfMeasure,')')"/></name>
          </Characteristic>
          <Type>abcd</Type>
        </xsl:when>
      </xsl:choose>
    </DescribedBy>
  </xsl:template>
  
</xsl:stylesheet>

See it working here : https://xsltfiddle.liberty-development.net/bEJbVrz

Note that since Characteristic and Type in your output are the same for the True and False cases you could take them out of the when conditions and have them for both cases, but I did it the same way your pseudocode is written.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...