Jump to content

xslt xml to xml trouble


jkamp

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
Link to comment
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.

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...