Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:45:29+00:00 2026-05-22T11:45:29+00:00

I have an XML schema that I need to extract values from but I

  • 0

I have an XML schema that I need to extract values from but I am not having much luck.

The souce schema is:

<Reading>
   <State>
      <StateValues>
         <Name>
         <Value>
      </StateValues>
   </State>
<Reading>

An example of the XML would be:

<Reading>
   <State>
      <StateValues>
         <Name>Reading1</Name>
         <Value>1234</Value>
      </StateValues>
      <StateValues>
         <Name>Reading2</Name>
         <Value>2345</Value>
      </StateValues>
   </State>
   <State>
      <StateValues>
         <Name>Reading3</Name>
         <Value>4321</Value>
      </StateValues>
   </State>
<Reading>

I need a result that is:

<Readings>
  <Reading1>1234</Reading1>
  <Reading2>2345</Reading2>
  <Reading3>4321</Reading3>
</Readings>

I have no control over the schemas.

I have been trying to use a xsl:foreach with an xsl:if that checks the name but I just get the first value repeated 3 times. Any tips? I’ve been trying to figure this out for 2 days and I am not an XSLT expert…

Thanks.
EDIT : Sorry for not posting my XSLT. I had tried several versions and didn’t think it would be useful. Here it is:

Blockquote

<xsl:element name="ns0:Readings">
   <xsl:element name="ns0:Current">
         <xsl:for-each select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']">               
            <xsl:choose>
               <xsl:when test="Name='TOTAL'">
            <xsl:element name="ns0:Total">
                <xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()='Total'" />
            </xsl:element>
           </xsl:when>
        </xsl:choose>
        <xsl:choose>
           <xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()='Reading1'">
        <xsl:element name="ns0:Reading1">
            <xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
            </xsl:element>
           </xsl:when>
        </xsl:choose>
        <xsl:choose>
           <xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading2'">
        <xsl:element name="ns0:Reading2">
            <xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
            </xsl:element>
           </xsl:when>
        </xsl:choose>
        <xsl:choose>
           <xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading3'">
        <xsl:element name="ns0:Reading3">
            <xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
            </xsl:element>
           </xsl:when>
        </xsl:choose>
        <xsl:choose>
           <xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading4'">
        <xsl:element name="ns0:Reading4">
            <xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
            </xsl:element>
           </xsl:when>
        </xsl:choose>
     </xsl:for-each>
   </xsl:element>
</xsl:element>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:45:30+00:00Added an answer on May 22, 2026 at 11:45 am

    foreach and if are usually the wrong approach in XSLT. It is far more effective to apply templates to patterns. In this case you can use two: the first sets up the root element and matches the elements you want to work with:

    <xsl:template match='/'>
      <Readings>
        <xsl:apply-templates select='Reading/State/StateValues'/>
      </Readings>
    </xsl:template>
    

    and a second that the above triggers when a match is found that will do the core work. The complexity here is creating an element dynamically which means using the {XPath} syntax to get the name from the input document:

    <xsl:template match='StateValues'>
      <xsl:element name='{Name}'>
        <xsl:value-of select='Value' />
      </xsl:element>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file, I need to extract values from it, and put
I have a XML schema that I will need to create Java classes for.
I need to generate classes from xml that doesn't provide a schema. I understand
I have an XML schema that includes multiple addresses: <xs:element name=personal_address maxOccurs=1> <!-- address
I have an XML schema that represents a product in a DB, and I
I have a custom XML schema defined for page display that puts elements on
I have a xml blob that's checked against a schema in sql 2005. My
I have to make a schema for an XML format that's already being used.
I need to create an XML schema definition (XSD) that describes Java objects. I
I have a number of XML schema that use JAXB (Java Architecture for XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.