arphex
arphex

Reputation: 180

Key in XML search with XSLT

I have XML Code:

<TestCases>
    <TestCase>
        <TestCaseElement>
            <Name><![CDATA[IP_EXTDEVICE]]></Name>
            <Tags>
                <Tag><![CDATA[Upperlimit]]></Tag>
            </Tags>
            <TaggedValues>
                <TaggedValue key="Upperlimit">
                    <value><![CDATA[4]]></value>
                </TaggedValue>
            </TaggedValues>
        </TestCaseElement>
    </TestCase>
</TestCases>

How can i select with XSLT the Value 4 ?


<xsl:template name="selectOwnTaggedValue">
    <xsl:value-of select="[normalize-space(value)]" />
</xsl:template>
<xsl:template match="/">
    <xsl:for-each select="TestCases/TestCase/TestCaseElement">
        <xsl:apply-templates select="selectOwnTaggedValue[TaggedValues[TaggedValue key="Upperlimit"]" />
    </xsl:for-each>
</xsl:template>

My XSL-Code is not valid because i dont know how to select the value 4 in my transformation

Thx in advance

Upvotes: 2

Views: 134

Answers (4)

arphex
arphex

Reputation: 180

Hi my solution is now:

in xsl:

<xsl:template name="Unit" > 
    <xsl:value-of select="TaggedValues/TaggedValue[@key='Unit']/value"/> 
</xsl:template>

<xsl:template name="Precision" > 
    <xsl:value-of select="TaggedValues/TaggedValue[@key='Precision']/value"/> 
</xsl:template>

<xsl:template name="Upperlimit" > 
    <xsl:value-of select="TaggedValues/TaggedValue[@key='Upperlimit']/value"/> 
</xsl:template>

<xsl:template name="Lowerlimit" > 
    <xsl:value-of select="TaggedValues/TaggedValue[@key='Lowerlimit']/value"/> 
</xsl:template>

called with

<xsl:call-template name="Unit"/>

on this xml:

<TestCaseElement>
        <Name><![CDATA[ini_Alle-Schütze_OFF_V1-0]]></Name>
        <Tags>
            <Tag><![CDATA[Upperlimit]]></Tag>
            <Tag><![CDATA[Precision]]></Tag>
            <Tag><![CDATA[Lowerlimit]]></Tag>
            <Tag><![CDATA[Unit]]></Tag>
        </Tags>

        <TaggedValues>
            <TaggedValue key="Upperlimit">
                <value><![CDATA[1]]></value>
            </TaggedValue>
            <TaggedValue key="Precision">
                <value><![CDATA[0]]></value>
            </TaggedValue>
            <TaggedValue key="Lowerlimit">
                <value><![CDATA[1]]></value>
            </TaggedValue>
            <TaggedValue key="Unit">
                <value><![CDATA[ok]]></value>
            </TaggedValue>
        </TaggedValues>
   </TestCaseElement>

and my desired output:

is the value that is in the respective value.

thank you sean, your solution with the @key was the right hint :-)

Upvotes: 0

arphex
arphex

Reputation: 180

I think to hold it generic it is better to call the template with a parameter. Am i thinking correct?

Ok this is my XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<TestCases>
<TestCase>
<TestCaseElement>
<Name><![CDATA[IP_EXTDEVICE]]></Name>
        <Tags>
            <Tag><![CDATA[Upperlimit]]></Tag>
            <Tag><![CDATA[Lowerlimit]]></Tag>
        </Tags>
        <TaggedValues>
            <TaggedValue key="Upperlimit">
                <value><![CDATA[4]]></value>
            </TaggedValue>
            <TaggedValue key="Lowerlimit">
                <value><![CDATA[1]]></value>
            </TaggedValue>
        </TaggedValues>

        <Role><![CDATA[TESTSTEP]]></Role>
</TestCaseElement>
<and many more TestCaseElements/>
</TestCase>
</TestCases>

Now i need in my output Upper and lower Limit and best would be in a generic template so i would try now:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output name="text" method="text" omit-xml-declaration="yes" encoding="ISO-8859-1" />
    <xsl:template name="OwnTaggedValues">
        <xsl:value-of select="TaggedValues/TaggedValue[@key='taggedvalue']/value" />
    </xsl:template>
    <xsl:template match="/">
        <xsl:for-each select="TestCases/TestCase/TestCaseElement[Role = 'TESTSTEP']">
            <xsl:call-template name="OwnTaggedValues">
                </xsl:with-param name="taggedvalue" select="Upperlimit">
            </xsl:call-template>
            <xsl:text>And now the Lowerlimit</xsl:text>
            <xsl:call-template name="OwnTaggedValues">
                </xsl:with-param name="taggedvalue" select="Lowerlimit">
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

This is really exactly my issue :-). Thank you in advance sean and all others

Upvotes: 0

Sean B. Durkin
Sean B. Durkin

Reputation: 12729

Try this...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:apply-templates select="*/TestCase"/>
</xsl:template>
      
<xsl:template match="TestCase">
  <xsl:value-of select="concat(
   'The upper limit for ',
  TestCaseElement/Name,
   ' is ',
  TestCaseElement/TaggedValues/TaggedValue[@key='Upperlimit']/value,
  '&#x0A;'
   )"/>
</xsl:template>

Update

The output from running this style-sheet against the sample input is ...

The upper limit for IP_EXTDEVICE is 4

Let me know if you want a different output.

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

Use:

/*/TaggedValues/TaggedValue[@key='Upperlimit']/value

This selects the element.

string(/*/TaggedValues/TaggedValue[@key='Upperlimit']/value)

This produces the string value of the first selected element.

/*/TaggedValues/TaggedValue[@key='Upperlimit']/value/text()

This selects all text-node childtren of all selected elements.

Upvotes: 0

Related Questions