Brice
Brice

Reputation: 67

Why did xsl:value select no text?

I'm trying to bring some data from an XML file to another that an XSL file will create. However, I'm stuck on a part of my XSL.

XML SOURCE

<?xml version="1.0" encoding="utf-8"?>
<newsMessage xmlns="http://iptc.org/std/nar/2006-10-01/">
    <itemSet>
        <packageItem guid="BLABLA" version="3" standard="NewsML-G2" standardversion="2.7" conformance="power">
            <contentMeta>
                <altId type="TEST:ID">TEST_ID</altId>
            </contentMeta>
        </packageItem>
    </itemSet>
</newsMessage>

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes" method="xml"/>
    <xsl:template match="/">
        <xsl:element name="asset">
                    <xsl:value-of select="newsMessage/itemSet/packageItem/contentMeta/altId[@name='TEST:ID']"/>
                </xsl:element>
    </xsl:template>
</xsl:stylesheet>

XML TARGET:

<?xml version="1.0" encoding="UTF-8"?>
<asset/>

I would like to get:

<?xml version="1.0" encoding="UTF-8"?>
<asset>TEST_ID<asset/>

I'm sure I'm missing something.. any help is appreciated.

Edit, here's the correct XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://iptc.org/std/nar/2006-10-01/">
    <xsl:output indent="yes" method="xml"/>
    <xsl:template match="/">
        <xsl:element name="asset">
                    <xsl:value-of select="newsMessage/itemSet/packageItem/contentMeta/altId[@type='TEST:ID']"/>
                </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Views: 194

Answers (2)

Jasper Huzen
Jasper Huzen

Reputation: 1573

altId[@name='TEST:ID'] should be altId[@type='TEST:ID']

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 117073

You are missing two things, actually:

  1. You are using the predicate [@name='TEST:ID'] - but the attribute's name is type;

  2. Your source XML is in a namespace; you need to declare the same namespace as xpath-default-namespace in your xsl:stylesheet element's start-tag.

Upvotes: 3

Related Questions