Jump to content
jkamp

xslt xml to xml trouble

Recommended Posts

Hi, I am currently trying to use an xslt transform to select data out of one xml file and put it in another. Now I have a little experience with xml, but am completely new to xslt. Here is what I have so far. Part of the XML File: <?xml version="1.0" encoding="UTF-8" ?><metadatareport xmlns="http://www.callassoftware.com/ns/pdftoolbox/1.0"'>http://www.callassoftware.com/ns/pdftoolbox/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.callassoftware.com/ns/pdftoolbox/1.0 metadatareport.xsd">

<documents>
<document>
<metadata>
<metadatavalue property_id="xmpMM:TV_SONumber">000_199_SO</metadatavalue>
<metadatavalue property_id="xmpMM:TV_ArtworkApproval">Yes</metadatavalue>
XSLT File: <?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="_00IRG_000_199_SO-IM_000_003_805_L-.xml" --><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"><xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/">
<xsl:element name="field-list">
<xsl:element name="field"> <xsl:element name="tag">SO Number</xsl:element>
<xsl:element name="value"><xsl:value-of select="metadatareport/documents/document/metadata/metadatavalue[@property_id=xmpMM:TV_SONumber]"/></xsl:element>
</xsl:element>
<xsl:element name="field">
<xsl:element name="tag">Artwork Approved</xsl:element>
<xsl:element name="value"><xsl:value-of select="metadatareport/documents/document/metadata/metadatavalue[@property_id=xmpMM:TV_ArtworkApproval]"/></xsl:element>
</xsl:element>
</xsl:element>
</xsl:template></xsl:stylesheet> This is the output I am getting: <?xml version="1.0" encoding="UTF-8"?><field-list>
<field>
<tag>SO Number</tag>
<value/>
</field>
<field>
<tag>Artwork Approved</tag>
<value/>
</field>
</field-list> Now the output for the most part is what I want. The problem is that value element in the output is blank, so there must be something wrong with my value-of statement. I have tried multiple things to try and fix it; however, I am very new to xslt, so I am not really sure how to fix this. So if anybody could help me figure out how to select those values from the xml file or give some advice on what I am doing wrong, it would be greatly appreciated. Thanks for your time. Edited by jkamp

Share this post


Link to post
Share on other sites

Which XSLT processor do you use?Are you sure you get the result you have posted with the samples you have posted? I could explain why the value-of expressions don't select anything, that is due to a namespace being used in the XML input, on the other hand with that the match="/metadatareport" shouldn't work either. As for fixing it, with XSLT 2.0 and an XSLT 2.0 processor using xpath-default-namespace="http://www.callassoftware.com/ns/pdftoolbox/1.0" on the xsl:stylesheet could help, as long as the input is as posted. If the root element is not in a namespace it might get more complex.

Share this post


Link to post
Share on other sites

Sorry, no that wasn't the result for the sample xslt file. That result is with using match="/". When using match="/metadatareport" I get values, but there are around 50 <metadatavalue> elements and I get the values of all 50 of them, not just the two that i wrote the value-of select statements for.

Share this post


Link to post
Share on other sites

You haven't told us which XSLT processor you use, assuming an XSLT 2.0 processor like Saxon 9, AltovaXML or XmlPrime you should be able to use your code against the input sample you have shown by doing e.g.

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0"  xpath-default-namespace="http://www.callassoftware.com/ns/pdftoolbox/1.0"> <xsl:template match="/metadatareport">  <fieldlist>    <field>       ...	  <value><xsl:value-of select="documents/document/metadata/metadatavalue[@property_id='xmpMM:TV_SONumber']"/></value>	 ...   </field>  </fieldlist></xsl:template> </xsl:stylesheet>

With an XSLT 1.0 processor you need to do e.g.

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0"  xmlns:df="http://www.callassoftware.com/ns/pdftoolbox/1.0"  exclude-result-prefixes="df"> <xsl:template match="/df:metadatareport">  <fieldlist>    <field>       ...	  <value><xsl:value-of select="df:documents/df:document/df:metadata/df:metadatavalue[@property_id='xmpMM:TV_SONumber']"/></value>	 ...   </field>  </fieldlist></xsl:template> </xsl:stylesheet>

That should give you an idea how to handle the namespace in the input with the respective XSLT versions.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...