0

[I've looked at similar questions but none I found answer this, please do not mark a duplicate without actually reading it]

How would I select a node using the contents of savetype in xslt.

eg in this example savetype=dexterity so I want the value of

<xsl:value-of select="/root/character/abilities/dexterity/bonus"/>

Is there anyway to do this without a <xsl:choose> statement, can I build the XPATH statement from the value of savetype

Sample file

<?xml version="1.0" encoding="iso-8859-1"?>
<root version="3.1" release="7|CoreRPG:3">
  <character>
     <abilities>
        <charisma>
           <bonus type="number">-1</bonus>
        </charisma>
        <constitution>
           <bonus type="number">2</bonus>
        </constitution>
        <dexterity>
           <bonus type="number">2</bonus>
        </dexterity>
     </abilities>
     <powers>
        <id-00005>
           <actions>
              <id-00001>
                 <savetype type="string">dexterity</savetype>
              </id-00001>
           </actions>
        </id-00005>
     </powers>
  </character>
</root>

1 Answer 1

2

can I build the XPATH statement from the value of savetype

Yes. Try:

<xsl:value-of select="/root/character/abilities/*[name()=//savetype]/bonus" />

Another option is to use a key. Define a key at the top level of your stylesheet as:

<xsl:key name="k" match="abilities/*" use="name()" />

then use:

<xsl:value-of select="key('k', //savetype)/bonus" />
Sign up to request clarification or add additional context in comments.

2 Comments

Could you explain what the * is for - almost looks like a pointer deference to me. I am guessing the part in [] is creating a node called whatever the value of savetype is. Also is // needed on savetype? Sorry - I'm new at xslt
@Adrian You are asking for an XPath tutorial, which I cannot provide in a comment. Briefly: * is wildcard that stands for any element, [] indicates a predicate, and in order to select the savetype element you need to provide a path to it - either a relative or an absolute one. Since the context is unknown, I have used an absolute one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.