xml - Selecting value of child element -


why <xsl:value-of select="p"/> not resulting in output of xml source’s p element?

also, how can prevent xmlns="" appearing on result’s trans-unit elements?

thanks.

source xml

<?xml version="1.0" encoding="utf-8"?> <!doctype svg public "-//w3c//dtd svg 20001102//en"    "http://www.w3.org/tr/2000/cr-svg-20001102/dtd/svg-20001102.dtd" [     <!entity ns_graphs "http://ns.adobe.com/graphs/1.0/">     <!entity ns_vars "http://ns.adobe.com/variables/1.0/">     <!entity ns_imrep "http://ns.adobe.com/imagereplacement/1.0/">     <!entity ns_custom "http://ns.adobe.com/genericcustomnamespace/1.0/">     <!entity ns_flows "http://ns.adobe.com/flows/1.0/"> <!entity ns_extend "http://ns.adobe.com/extensibility/1.0/"> ]> <svg> <variablesets  xmlns="&ns_vars;">     <variableset  varsetname="binding1" locked="none">         <variables>             <variable  category="&ns_flows;" trait="textcontent" varname="variable1"></variable>             <variable  category="&ns_flows;" trait="textcontent" varname="variable2"></variable>             <variable  category="&ns_flows;" trait="textcontent" varname="variable3"></variable>             <variable  category="&ns_flows;" trait="textcontent" varname="variable4"></variable>         </variables>         <v:sampledatasets  xmlns:v="&ns_vars;" xmlns="&ns_custom;">             <v:sampledataset  datasetname="english">                 <variable1>                     <p>introduction</p>                 </variable1>                 <variable2>                     <p>step</p>                 </variable2>                 <variable3>                     <p>3</p>                 </variable3>                 <variable4>                     <p>anonymous answer</p>                 </variable4>             </v:sampledataset>         </v:sampledatasets>     </variableset> </variablesets> </svg> 

xslt

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:v = "http://ns.adobe.com/variables/1.0/"     exclude-result-prefixes="v">      <xsl:output method="xml" encoding="utf-8" indent="yes"/>      <xsl:param name="file"/>     <xsl:param name="source-lang" select="'en-gb'"/>     <xsl:param name="target-lang"/>      <xsl:template match="/">         <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">             <file original="{$file}" source-lanuage="{$source-lang}" target-language="{$target-lang}">                 <xsl:apply-templates select="//v:sampledataset/*"/>             </file>         </xliff>     </xsl:template>      <xsl:template match="//v:sampledataset/*">         <trans-unit id="{local-name()}">             <source xml:lang="{$source-lang}"><xsl:value-of select="p"/></source> <!-- no value output -->             <target xml:lang="{$target-lang}"></target>         </trans-unit>     </xsl:template>  </xsl:stylesheet> 

result

<?xml version="1.0" encoding="utf-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">   <file original="ig-001" source-lanuage="en-gb" target-language="de-de">     <trans-unit xmlns="" id="variable1">       <source xml:lang="en-gb"/>       <target xml:lang="de-de"/>     </trans-unit>     <trans-unit xmlns="" id="variable2"> <!-- unwanted xmlns attribute -->       <source xml:lang="en-gb"/> <!-- output needed here -->       <target xml:lang="de-de"/>     </trans-unit>     <trans-unit xmlns="" id="variable3">       <source xml:lang="en-gb"/>       <target xml:lang="de-de"/>     </trans-unit>     <trans-unit xmlns="" id="variable4">       <source xml:lang="en-gb"/>       <target xml:lang="de-de"/>     </trans-unit>   </file> </xliff> 

desired result

<?xml version="1.0" encoding="utf-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">   <file original="ig-001" source-lanuage="en-gb" target-language="de-de">     <trans-unit id="variable1">       <source xml:lang="en-gb">introduction</source>       <target xml:lang="de-de"/>     </trans-unit>     <trans-unit id="variable2">       <source xml:lang="en-gb">step</source>       <target xml:lang="de-de"/>     </trans-unit>     <trans-unit id="variable3">       <source xml:lang="en-gb">3</source>       <target xml:lang="de-de"/>     </trans-unit>     <trans-unit id="variable4">       <source xml:lang="en-gb">anonymous answer</source>       <target xml:lang="de-de"/>     </trans-unit>   </file> </xliff> 

solution

p element value: per @larsh’s answer, namespace issue. added xmlns:custom = "http://ns.adobe.com/genericcustomnamespace/1.0/" declaration xsl:stylesheet element , used custom:p instead of p:

<xsl:value-of select="custom:p"/> 

xmlns="": setting default namespace xmlns attribute on xsl:stylesheet element instead of manually on root element handles this.

new xslt in full:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns="urn:oasis:names:tc:xliff:document:1.2"     xmlns:vars = "http://ns.adobe.com/variables/1.0/"     xmlns:custom = "http://ns.adobe.com/genericcustomnamespace/1.0/"     exclude-result-prefixes="vars custom">      <xsl:output method="xml" encoding="utf-8" indent="yes"/>      <xsl:param name="file"/>     <xsl:param name="source-lang" select="'en-gb'"/>     <xsl:param name="target-lang"/>      <xsl:template match="/">         <xliff version="1.2">             <file original="{$file}" source-lanuage="{$source-lang}" target-language="{$target-lang}">                 <xsl:apply-templates select="//vars:sampledataset/*"/>             </file>         </xliff>     </xsl:template>      <xsl:template match="//vars:sampledataset/*">         <trans-unit id="{local-name()}">             <source xml:lang="{$source-lang}"><xsl:value-of select="custom:p"/></source>             <target xml:lang="{$target-lang}"></target>         </trans-unit>     </xsl:template>  </xsl:stylesheet> 

why <xsl:value-of select="p"/> not resulting in output of xml source’s p element?

in template

<xsl:template match="//v:sampledataset/*">     <trans-unit id="{local-name()}">         <source xml:lang="{$source-lang}"><xsl:value-of select="p"/></source>  

the context node child of <v:sampledataset>, such <variable1>. in source xml, latter has <p> element children in namespace identified uri http://ns.adobe.com/genericcustomnamespace/1.0/. because <p> has no namespace prefix, uses default namespace, established nearest ancestor has default namespace declaration.

<v:sampledatasets  ... xmlns="&ns_custom;"> 

however xslt asking p elements in no namespace. hence lack of output.

also, how can prevent xmlns="" appearing on result’s trans-unit elements?

you can't guarantee how namespace declarations appear in serialized output. however, can specify namespace each element in. since want <trans-unit> elements in oasis namespace, need specify in xslt; , since have ancestor in output xml has oasis namespace default namespace, may serialized no namespace prefix , no namespace declaration attribute. can tell xslt output <trans-unit> elements in oasis namespace same way did <xliff> elements:

   <trans-unit xmlns="urn:oasis:names:tc:xliff:document:1.2" id="{local-name()}"> 

that put trans-unit elements correct namespace in result xml, important thing downstream processing (and isn't correct @ moment). namespace declarations in result xml nice, that's not guaranteed. however, there details placing namespace nodes don't remember, maybe else can give better answer on that.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -