1

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>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Brice
  • 67
  • 2
  • 13

2 Answers2

3

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.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thanks, that's exactly what I missed. Good eye for the predicate too! Will edit with the correct code. – Brice Sep 23 '20 at 22:32
1

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

Jasper Huzen
  • 1,513
  • 12
  • 26