0

I'm a XSLT beginner, but managed to write one successful transformation in the recent past.

This time, however I'm having trouble "lifting off", i.e.: For some odd reason the XML root element isn't matched when I name it. However it will be matched when I use just /.

Let's start seeing the outline of the XML document (I didn't write that; "professional companies" create such):

<?xml version="1.0" encoding="utf-8"?>
<PartnerMasterDataSet xmlns="http://tempuri.org/PartnerMasterDataSet.xsd">
  <Partner>
    <Id>40</Id>
<!-- ... -->
  </Partner>
</PartnerMasterDataSet>

The final line has no trailing EOL sequence (in case that matters). There are multiple elements like Id (and multiple Partners).

What I tried in my XSL:

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

<xsl:template match="/PartnerMasterDataSet">
  <table>
<!-- <xsl:apply-templates select="Partner" /> -->
  </table>
</xsl:template>
</xsl:stylesheet>

I'm using xsltproc from libxslt-tools-1.1.28 to process the input, in case it's important.

When using match="/", I get a match (verbose messages) like this:

xsltProcessOneNode: applying template '/' for /
xsltApplySequenceConstructor: copy text root

However when I use match="/PartnerMasterDataSet" (or match="//PartnerMasterDataSet"), the I don't get a match, which I don't understand.

Then the messages are:

xsltProcessOneNode: no template found for /
xsltProcessOneNode: no template found for PartnerMasterDataSet
U. Windl
  • 3,480
  • 26
  • 54

1 Answers1

1

just add the namespace to your script...

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xx="http://tempuri.org/PartnerMasterDataSet.xsd">
<xsl:output indent="yes" />
<xsl:template match="/xx:PartnerMasterDataSet">
  <table>
  </table>
</xsl:template>
</xsl:stylesheet>
Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21
  • Oh! The original namespace given (not locatable) was nonsense, and I have to repeat that nonsense in my script!? Maybe your answer could explain the importance (i.e. the role) of the namespace for matching. – U. Windl Jun 06 '23 at 07:48
  • there's a default namespace (no prefix) declared in the source xml, so to match the root you'll have to declare it in xslt too. Or use wildcards like /*/Partner to ignore the root name – Marc Stroebel Jun 06 '23 at 07:58
  • @U.Windl Namespaces are not "nonsense". Do some reading, perhaps starting with: https://en.wikipedia.org/wiki/XML_namespace. – michael.hor257k Jun 06 '23 at 08:04
  • @MarcStroebel Default namespaces are inherited. `/*/Partner` will not select anything because `Partner` is in a namespace too. – michael.hor257k Jun 06 '23 at 11:34
  • @michael.hor257k I was referring to "http://tempuri.org/". I never understood what rationale is behind specifying a namespace as URL when that URL does not exist (any more). – U. Windl Jun 06 '23 at 13:20
  • @U.Windl The rationale is that the URL belongs to the XML author, so they can be sure it is not used by anyone else. There is no requirement that the namespace is a URL or even in the form of a URL - for example, `xmlns="urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a"` is a perfectly valid namespace declaration. Basically, any unique string will do, though the [specification](https://www.w3.org/TR/REC-xml-names/) mandates it to be a URI. – michael.hor257k Jun 06 '23 at 13:41
  • @michael.hor257k Thanks for explaining. Still while seemingly "defining" a namespace such way, you have no chance to get an actual namespace or DTD definition automatically that way, and I wonder how to build a correct XSLT from such info. That's why I called it "nonsense". – U. Windl Jun 07 '23 at 06:16
  • Sorry, I don't know what you mean. You are not *'seemingly "defining" a namespace such way'*. You are declaring an actual namespace. DTD definitions have nothing to do with it. You are simply extending the names of the elements. Think of it as adding a last name to each child in class. The last name is meaningless - but it is not "nonsense". – michael.hor257k Jun 07 '23 at 06:56