【问题标题】:How to map value from xml to XSLT如何将值从 xml 映射到 XSLT
【发布时间】:2021-04-12 03:01:25
【问题描述】:

我正在尝试将数据从 xml 填充到 xslt n 无法做到这一点,因为我不知道 xslt 和 xpath。即使我尽力了我的水平(使用 concat() 方法)但不能。你能帮我做下面的映射吗? :----

Xml:----(一段代码)

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

试图达到以下目标:--

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

映射条件:---

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

这是我迄今为止尝试过的:--

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

【问题讨论】:

  • 如果您不了解 XSLT 和 XPath,那么您应该了解这些并做一些教程。 “从 XML 到 XSLT”到底是什么意思?在你的代码 sn-p 中这是什么语言?它既不是 Java 也不是 XSLT。请尽量说明您的实际问题是什么。你的问题很混乱。
  • @vanje 使用 concat () 方法尝试了很多,但没有成功。所以寻求帮助
  • 是的,你已经提到了。但这并没有让事情变得更清楚。你根本没有回答我的问题。也许你应该阅读How do I ask a good question?create a Minimal, Reproducible Example。现在完全不清楚你想要完成什么。
  • @priyranjan 那么请发布您失败的 XSLT 而不是一些难以理解的伪代码!
  • @Gyro Gearless..到目前为止我尝试过的,现在添加。

标签: java xml xslt xpath


【解决方案1】:

以下是使用 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>

在这里看到它的工作:https://xsltfiddle.liberty-development.net/bEJbVrz

请注意,由于 True 和 False 情况下输出中的 Characteristic 和 Type 是相同的,因此您可以将它们从 when 条件中取出并在两种情况下都使用它们,但我以与编写伪代码相同的方式进行操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2018-01-19
    • 2013-05-04
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    相关资源
    最近更新 更多