Replacing one namespace with another - node.js

In the below input XML I am getting ns1 and ns2 prefixes.
In the output the namespace ns1 should be replaced with dh and the namespace ns2 should be replaced with sk.
Can anyone please help on this.
<?xml version="1.0" encoding="UTF-8" ?>
<sh:sampleDocument schemaVersion="" creationDate=""xmlns:sh="htpp://sample1.com">
<sampleHeader>
<ns1:sampledocumentheader xmlns:ns1="htpp://sample2.com">
<ns1:HeaderVersion />
<ns1:ContactInformation>
<ns1:Contact />
<ns1:EmailAddress />
<ns1:FaxNumber />
<ns1:TelephoneNumber />
<ns1:ContactTypeIdentifier />
</ns1:ContactInformation>
</ns1:sampledocumentheader>
</sampleHeader>
<sampleBody>
<sampleList>
<sampleEvent>
<Time />
<action />
<ns2:sampleExtension xmlns:ns2="htpp://sample3.com">
<ns2:Value />
<ns2:Number />
</ns2:sampleExtension>
</sampleEvent>
</sampleList>
</sampleBody>
</sh:sampleDocument>

There should be no need to do this. The choice of a namespace prefix is completely arbitrary. If the target application requires a specific prefix to be used, then it's the target application that needs to be fixed, not your XML.
Anyway, try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="htpp://sample2.com"
xmlns:ns2="htpp://sample3.com"
exclude-result-prefixes="ns1 ns2">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:*">
<xsl:element name="dh:{local-name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="ns2:*">
<xsl:element name="sk:{local-name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Related

pattern for generating JSON output

I'm trying to map XML to JSON using XSLT 3.0
my broad plan is to take the input, map it to some elements in memory, and then map that to 'map's and 'array's to by applying templates and then letting the XSLT serialise that as JSON.
Here is my initial effort:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
version="3.0">
<xsl:output method="json" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="dsl" as="element()">
<epg lastBuildDate="10/4/2019 9:46:00 AM">
</epg>
</xsl:variable>
<xsl:variable name="output">
<xsl:apply-templates select="$dsl" mode="interpret"/>
</xsl:variable>
<xsl:sequence select="$output"/>
</xsl:template>
<xsl:template match="epg" mode="interpret">
<xsl:sequence select="map {
'lastBuildDate' : #lastBuildDate
}"/>
</xsl:template>
</xsl:stylesheet>
sadly I get
Cannot add a map to an XDM node tree
in the 'interpret' template.
The error occurs with
<xsl:variable name="output">
<xsl:apply-templates select="$dsl" mode="interpret"/>
</xsl:variable>
as that is an implicit use of
<xsl:variable name="output">
<xsl:document>
<xsl:apply-templates select="$dsl" mode="interpret"/>
</xsl:document>
</xsl:variable>
While maps can contain nodes as values, nodes can't contain maps.
For that simple example it should work to use
<xsl:variable name="output" as="item()*">
<xsl:apply-templates select="$dsl" mode="interpret"/>
</xsl:variable>
or even
<xsl:variable name="output" as="item()">
<xsl:apply-templates select="$dsl" mode="interpret"/>
</xsl:variable>
or
<xsl:variable name="output" as="map(*)">
<xsl:apply-templates select="$dsl" mode="interpret"/>
</xsl:variable>
this works, I'll mark the other answer as correct though there is a 2nd issue where you have to 'cast' the attribute to a string
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
version="3.0">
<xsl:output method="json" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="dsl" as="element()">
<epg lastBuildDate="10/4/2019 9:46:00 AM">
</epg>
</xsl:variable>
<xsl:variable name="output" as="map(*)*">
<xsl:apply-templates select="$dsl" mode="interpret"/>
</xsl:variable>
<xsl:sequence select="$output"/>
</xsl:template>
<xsl:template match="epg" as="map(*)" mode="interpret">
<xsl:sequence select="map {
'lastBuildDate' : #lastBuildDate => xs:string()
}"/>
</xsl:template>
</xsl:stylesheet>

How can I handle error Exception with try/catch using xslt 3.0

How can I handle error Exception with try/catch using xslt 3.0. I am finding in xml number Element. If number Element not find in parent product then generate a file error.txt and how to write the Exception.
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<product dept="WMN">
<name language="en">Fleece Pullover</name>
<colorChoices>navy black</colorChoices>
</product>
<product dept="ACC">
<number>563</number>
<name language="en">Floppy Sun Hat</name>
</product>
<product dept="ACC">
<number>443</number>
<name language="en">Deluxe Travel Bag</name>
</product>
<product dept="MEN">
<number>784</number>
<name language="en">Cotton Dress Shirt</name>
<colorChoices>white gray</colorChoices>
<desc>Our <i>favorite</i> shirt!</desc>
</product>
</catalog>
Using XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs"
version="3.0">
<xsl:template match="/">
<xsl:stream href="books.xml">
<xsl:iterate select="catalog">
<xsl:result-document href="out.xml" omit-xml-declaration="no" indent="yes">
<xsl:copy>
<xsl:for-each select="product">
<xsl:try>
<xsl:copy-of select="number"/>
<xsl:catch>
<xsl:result-document href="error.txt">
<xsl:message>Element number not given in <xsl:value-of select="product/#dept"/></xsl:message>
<error code="{$err:code}" message="{$err:description}"/>
</xsl:result-document>
</xsl:catch>
</xsl:try>
</xsl:for-each>
</xsl:copy>
</xsl:result-document>
</xsl:iterate>
</xsl:stream>
</xsl:template>
</xsl:stylesheet>
My requirement is how can I uses try/catch when not find the number element then make a file with error.
Try this to generate error message in log file by taking the value in variable in then check some conditon:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs"
version="3.0">
<xsl:template match="/">
<xsl:stream href="books.xml">
<xsl:iterate select="catalog">
<xsl:result-document href="out.xml" omit-xml-declaration="no" indent="yes">
<xsl:copy>
<xsl:for-each select="product">
<xsl:variable name="num" select="if (number) then (number) else ('aaaa')"/>
<xsl:try>
<number>
<xsl:value-of select="format-number($num, '############')"/>
</number>
<xsl:catch>
<xsl:result-document href="error.log" omit-xml-declaration="yes">
<xsl:value-of select="concat(' Element number not given in ', #dept)"/>
<xsl:value-of select="concat(' Error code: ', $err:code, ' and Error Desc is: ', $err:description)"/>
</xsl:result-document>
</xsl:catch>
</xsl:try>
</xsl:for-each>
</xsl:copy>
</xsl:result-document>
</xsl:iterate>
</xsl:stream>
</xsl:template>
</xsl:stylesheet>

Print xml nodes for each element in a single row

I need to print the category and hours values from all nodes in this xml as comma separated values in a single row through xslt -
XML
<?xml version="1.0" encoding="UTF-8"?>
<course>
<subcourse>
<code>ABC</code>
<name>REFCOURSE</name>
<date>Date</date>
<category>SDF</category>
<hours>7</hours>
</subcourse>
<subcourse>
<code>DEF</code>
<name>ORIGCOURSE</name>
<date>Date</date>
<category>UIT</category>
<hours>9</hours>
</subcourse>
</course>
Output needed -
SDF,7,UIT,9
By taking help from stakoverflow, here's what I've done so far -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:param name="range-1-begin" select="4"/>
<xsl:param name="range-1-end" select="5"/>
<xsl:param name="range-2-begin" select="6"/>
<xsl:param name="range-2-end" select="7"/>
<xsl:output method="text" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="subcourse">
<info><xsl:apply-templates/></info>
</xsl:template>
<xsl:template match="subcourse">
<xsl:if test = "not(position()= 1)">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Output - ABCREFCOURSEDateSDF7,DEFORIGCOURSEDateUIT9
I need it to iterate through every subcourse and pick category and hours if exist. I could not find how to pick only category and hours.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="//subcource">
<xsl:if test="category">
<xsl:if test = "not(position()=1)">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="category"/><xsl:text>,</xsl:text><xsl:value-of select="hours"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

XSLT 1.0 How to use xsl:key with document() function

I'm trying to use xsl:key to lookup items in an external XML document, using the XSL document() function. I am able to get the xsl:key part to work if, instead of using document(), I just merge the two XML files (using XmlDocument in C#). However both XML files are very large, and I'm starting to get "out of memory" errors in some cases. Also I need to be able to use xls:key, otherwise the process takes hours.
In XSLT 2.0, I believe you can do something like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="lookupDoc" select="document('CodeDescriptions.xml')" />
<xsl:key name="LookupDescriptionByCode" match="Code/#description" use="../#code" />
<xsl:template match="ItemCode">
<xsl:call-template name="MakeSpanForCode">
<xsl:with-param name="code" select="text()" />
</xsl:call-template>
</xsl:template>
<xsl:template name="MakeSpanForCode">
<xsl:param name="code" />
<xsl:element name="span">
<xsl:attribute name="title">
<xsl:value-of select="$lookupDoc/key('LookupDescriptionByCode', $code)" />
</xsl:attribute>
<xsl:value-of select="$code" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
How do you accomplish this in XSLT 1.0 though?
You have two possibilities:
without key
<xsl:template name="MakeSpanForCode">
<xsl:param name="code" />
<xsl:element name="span">
<xsl:attribute name="title">
<xsl:value-of select="$lookupDoc/*/Code[#code = $code]/#description" />
</xsl:attribute>
<xsl:value-of select="$code" />
</xsl:element>
</xsl:template>
with key
The key definition applies to all documents, but you need to change the context node before using the key() function:
<xsl:template name="MakeSpanForCode">
<xsl:param name="code" />
<xsl:element name="span">
<xsl:attribute name="title">
<!-- trick: change context node to external document -->
<xsl:for-each select="$lookupDoc">
<xsl:value-of select="key('LookupDescriptionByCode', $code)"/>
</xsl:for-each>
</xsl:attribute>
<xsl:value-of select="$code" />
</xsl:element>
</xsl:template>
Also see two great mailing list answers from Mike Kay and Jeni Tennison on this topic

SharePoint Designer Data View- XSLT Count

I am working with a data view web part in SPD 2010. My xml structure is as follows:
<ProjectGroups>
<ProjectGroup>
<GroupID>1</GroupID>
<ProjectName>Project 1</ProjectName>
</ProjectGroup>
<ProjectGroup>
<GroupID>2</GroupID>
<ProjectName>Project 2</ProjectName>
</ProjectGroup>
<ProjectGroup>
<GroupID>2</GroupID>
<ProjectName>Project 3</ProjectName>
</ProjectGroup>
</ProjectGroups>
This is a rollup web part, so what I am looking to do is get a count of Projects under each Project group. For my example above, Group ID 1 has 1 project, Group ID 2 has 2. I am sure there's a way to do this, but I'm sort of learning xslt on the fly, so I'm not sure exactly what to do. Any help is appreciated. Thanks.
This style-sheet ...
<?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:key name="groups" match="ProjectGroup" use="GroupID" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ProjectGroup[generate-id()=generate-id(key('groups',GroupID)[1])]">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:attribute name="count-of-projects">
<xsl:value-of select="count(key('groups',GroupID))" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
... when applied to your input, will produce ...
<?xml version="1.0" encoding="utf-8"?>
<ProjectGroups>
<ProjectGroup count-of-projects="1">
<GroupID>1</GroupID>
<ProjectName>Project 1</ProjectName>
</ProjectGroup>
<ProjectGroup count-of-projects="2">
<GroupID>2</GroupID>
<ProjectName>Project 2</ProjectName>
</ProjectGroup>
<ProjectGroup>
<GroupID>2</GroupID>
<ProjectName>Project 3</ProjectName>
</ProjectGroup>
</ProjectGroups>

Resources