Jump to content

Issue with Namespace in XML


GodFather

Recommended Posts

Hi, My problem is similar to this one: http://w3schools.inv...showtopic=40313 I have a XML file that looks more ot less like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml-stylesheet type="text/xsl" href="A1Telecom.xslt"?><CardOrder xmlns="http://rise-world.com/bonanza/Gemalto_card_perso_request" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Date="2012-03-26Z" Time="15:10:35Z" TransportKeyId="MKA-RISE.TEST.NFC.TK.01" xsi:schemaLocation="http://rise-world.com/bonanza/Gemalto_card_perso_request TSM_Gemalto_CardOrder_request_v1_0.xsd">	<Batch>		<CardProfile UniqueID="2A864886FC6B6402FE" ProfileVersion="1.0.1"/>		<Card UniqueCardIdentifier="000197E06958A918" RandomizeUID="true">			<Infos>				<Info Name="LetterPortfolio" Value="203"/>				<Info Name="MSISDN" Value=""/>				<Info Name="Gender" Value=""/>				<Info Name="AcademicTitle" Value=""/>				<Info Name="FirstName" Value=""/>				<Info Name="LastName" Value=""/>				<Info Name="PreparedName" Value=""/>				<Info Name="Street" Value=""/>				<Info Name="ZIP" Value=""/>				<Info Name="Town" Value=""/>				<Info Name="Country" Value=""/>				<Info Name="ActivationCode" Value=""/>				<Info Name="Glyph" Value=""/>			</Infos>			<CardDefinition>				<ApplicationProfile ProfileID="2A864886FC6B64010A" ProfileVersion="1.0.0">					<KeyValue Name="S-ENC" KeyId="ISD_1">						<Component Encoding="HEX" Value="" KCV="74C20717"/>					</KeyValue>					<KeyValue Name="S-MAC" KeyId="ISD_2">						<Component Encoding="HEX" Value="" KCV="84ECE56D"/>					</KeyValue>					<KeyValue Name="DEK" KeyId="ISD_3">						<Component Encoding="HEX" Value="" KCV="A73BF930"/>					</KeyValue>				</ApplicationProfile>				<ApplicationProfile ProfileID="2711" ProfileVersion="0.0.1">					<KeyValue Name="SK-WT" KeyId="PAY_W_KEY1">						<Component Encoding="HEX" Value="" KCV="1F1F7DF2" KeyIndex="00"/>					</KeyValue>					<KeyValue Name="SK-WBE" KeyId="PAY_W_KEY2">						<Component Encoding="HEX" Value="" KCV="2B896643"/>					</KeyValue>					<KeyValue Name="MK-TW" KeyId="PAY_W_KEY3">						<Component Encoding="HEX" Value="" KCV="135668B9"/>					</KeyValue>					<KeyValue Name="MK-BE-TP" KeyId="PAY_W_KEY4">						<Component Encoding="HEX" Value="" KCV="BA0FE564"/>					</KeyValue>					<KeyValue Name="MK-ID-ENC" KeyId="PAY_W_KEY5">						<Component Encoding="HEX" Value="" KCV="D6440F0D"/>					</KeyValue>					<DataElement Name="W-EntityId" ElementId="PAY_W_ID" Value="2938fe4528" Encoding="HEX"/>					<DataElement Name="W-Limit" ElementId="PAY_W_LIMIT" Value="001388" Encoding="HEX"/>					<DataElement Name="W-EndOfValidity" ElementId="PAY_W_EOV" Value="40f5" Encoding="HEX"/>					<DataElement Name="W-HomeCurrency" ElementId="PAY_W_HOME_CUR" Value="455552" Encoding="HEX"/>					<DataElement Name="W-CardholderPermissions" ElementId="PAY_W_PERM_CH" Value="00" Encoding="HEX"/>					<DataElement Name="W-KeyGeneration" ElementId="PAY_W_KEY_GEN" Value="00" Encoding="HEX"/>					<DataElement Name="FUSE-Response" ElementId="FUSE_RESP" Value="" Encoding="HEX"/>				</ApplicationProfile>				<ApplicationProfile ProfileID="2713" ProfileVersion="0.0.1">					<KeyValue Name="MK-UCID" KeyId="UCID_KEY">						<Component Encoding="HEX" Value="" KCV="695AFDBD" KeyGeneration="0000"/>					</KeyValue>					<DataElement Name="UCID" ElementId="UCID" Value="" Encoding="HEX"/>					<DataElement Name="FUSE-Response" ElementId="FUSE_RESP" Value="307f37986c62533f14ad54050f57cfbf" Encoding="HEX"/>				</ApplicationProfile>				<ApplicationProfile ProfileID="2710" ProfileVersion="0.0.1">					<KeyValue Name="NM-RSA-priv-key" KeyId="VDV_1">						<Component Encoding="HEX" Value=""/>					</KeyValue>					<DataElement Name="NM-CVC-Cert" ElementId="VDV_2" Value="" Encoding="HEX"/>				</ApplicationProfile>			</CardDefinition>		</Card>	</Batch></CardOrder>

As You can see it has a default namespace defined:

xmlns="http://rise-world.com/bonanza/Gemalto_card_perso_request"

When above line appears, my xslt transformation does not work. If I only add ":xsl" to be like:

xmlns:xsl="http://rise-world.com/bonanza/Gemalto_card_perso_request"

Everything is perfectly fine. Here is my xslt code:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="text" encoding="UTF-8" />   <xsl:template match="/">   <xsl:text>TransportKeyId=</xsl:text>   <xsl:value-of select="CardOrder/@TransportKeyId"/><xsl:text></xsl:text>   <xsl:for-each select="CardOrder/Batch/Card">   <xsl:text>UniqueCardIdentifier=</xsl:text>   <xsl:value-of select="@UniqueCardIdentifier"/><xsl:text>|</xsl:text>  </xsl:for-each>  </xsl:template></xsl:stylesheet>

Can You help?

Link to comment
Share on other sites

In the XSLT, remove the part

xmlns="http://www.w3.org/1999/XSL/Transform"

Add another "xmlns" attribute, using some prefix (any prefix other than "xsl" really) and associate that with the namespace from the file. Like for example:

xmlns:r="http://rise-world.com/bonanza/Gemalto_card_perso_request"

And then edit all the elements in all of your XPath expressions to use the prefix you specified. Keep the attributes without a prefix, i.e.

   <xsl:value-of select="r:CardOrder/@TransportKeyId"/><xsl:text></xsl:text>   <xsl:for-each select="r:CardOrder/r:Batch/r:Card">

Link to comment
Share on other sites

Thanks a lot, It actually worked without any other issues. Is there any solution when in XML file default namespace is variable and changes from file to file? How it can be remapped in XSLT? Regards,GodFather

Link to comment
Share on other sites

You could use the namespace-uri() and local-name() functions to target nodes in an arbitrary namespace.Combined with string functions, you can target nodes with a namespace/local-name matching a pattern.For example:

/*[local-name() = 'CardOrder' and starts-with(namespace-uri(), 'http://rise-world.com/bonanza/')]/@TransportKeyId

will select the "TransportKeyId" attribute of a "CardOrder" element in any namespace that starts with "http://rise-world.com/bonanza/".Note that this will most likely make the XSLT transformation slower, since every node needs to be matched at runtime (whereas XPath calls not using "*" can be matched while the XSLT is being compiled).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...