1

As part of an XSLT 1.0 transformation, I need to match an element whose local-name() is (say) fred. Obviously in this case there might be a namespace in effect or there might not be.

Within that match, I need to create an element whose local name should be location.

Is <xsl:element> sufficient for this purpose? Should I explicitly set its namespace attribute? Is there another way to create an element whose namespace is set appropriately, if there is one, and omitted if there is not?

1
  • Your question is not clear. <xsl:element> controls the output, it has nothing to do with matching the input. Is there a difference in the required output if the matched node is in a namespace or if it isn't? Commented May 26, 2015 at 17:41

1 Answer 1

1

Yes, you can use *[local-name() = 'fred'] to match an element with a local name of "fred" regardless of its namespace.

Within that match, I need to create an element whose local name should be location.

Here, your question becomes a bit unclear. The namespace (or lack of namespace) of an element you create in an output document is up to you; it needn't have any tie to the input document. If it should in some way be tied to a namespace found in the input document, you have to say from which part of the input document the namespace should come.

Update per OP comment below:

I want to use the namespace of parent fred element, whatever it may be, or if it is null/empty.

Ah, ok, this example may help then. Given this XML input document

<root xmlns="http://www.example.com">
  <fred/>
</root>

the following XSLT

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/*[local-name() = 'root']/*[local-name() = 'fred']">
    <xsl:element name="location"
                 namespace="{namespace-uri()}"/>
  </xsl:template>

</xsl:stylesheet>

will produce this output XML

<?xml version="1.0" encoding="UTF-8"?>
  <location xmlns="http://www.example.com"/>

which uses the namespace of the fred element from the input XML.

And when given this XML input document

<root>
  <fred/>
</root>

will produce this output XML

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

where location is in no namespace.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I want to use the namespace of parent fred element, whatever it may be, or if it is null/empty. Does that help? It is sounding like I should be referring to a variable that is computed from the parent element's namespace, possibly with some conditional around it to handle the case where there is no parent element namespace. Am I on the right track?

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.