 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 21st, 2011, 05:18 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Creating Dynamic Elements with Values from Other Element values
I am looking for generic template where it uses an element value into a tag and another element value as a field value.
here is source
Code:
<attributeList count="71">
<attribute>
<name>name1</name>
<string>Value1</string>
</attribute>
<attribute>
<name>name2</name>
<integer>value2</integer>
</attribute>
<attribute>
<name>name3</name>
<boolean>value3</boolean>
</attribute>
<attribute>
<name>name4</name>
<unknown>value4</unknown>
</attribute>
<attribute>
<name>name5</name>
<datetime>value5</datetime>
</attribute>
</attributeList>
output
Code:
<name1>value1<name1>
<name2>value2<name2>
<name3>value3<name3>
<name4>value4<name4>
<name5>value5<name5>
Does this code works? please advice, I have some bugs and testing around at this time
Code:
<xsl:element name="{../attributeList/attribute/name}">
<xsl:value-of select="../attributeList/attribute/string | ../attribute/integer | ../attribute/boolean | ../attribute/unknown | ../attribute/datetime"/>
</xsl:element>
|
|

February 21st, 2011, 07:34 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
It's not possible to say what your code does without knowing its context - but I can't think of any context in which it would work, in fact, the code seems to betray a lack of understanding of the importance of context.
This is a very simple bit of XSLT, it just needs
Code:
<xsl:template match="attribute">
<xsl:element name="{name}">
<xsl:value-of select="*[2]"/>
</xsl:element>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

February 22nd, 2011, 12:35 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the Source with Context
I have previous posting which includes original source of XML structure
XSL Loop with counter and concat
Here is the context
/ematrix/creationProperties/businessObject/attributeList/attribute
here is the part of source below as input
Code:
<attributeList count="32">
<attribute>
<name>Complaint Originator</name>
<string>super13</string>
</attribute>
<attribute>
<name>MPG Complaint Receipt Date</name>
<datetime>2010-08-20T05:00:00Z</datetime>
</attribute>
<attribute>
<name>MPG Complaint Reopen Count</name>
<integer>0</integer>
</attribute>
<attribute>
<name>MPG Complaint TPM Notification Date</name>
<unknown></unknown>
</attribute>
<attribute>
<name>MPG MDC Medical</name>
<boolean>TRUE</boolean>
</attribute>
Here is the output as required
Code:
<Complaint Originator>super13</Complaint Originator>
<MPG Complaint Receipt Date>2010-08-20T05:00:00Z</MPG Complaint Receipt Date>
<MPG Complaint Reopen Count>0</MPG Complaint Reopen Count>
<MPG Complaint TPM Notification Date></MPG Complaint TPM Notification Date>
<MPG MDC Medical>TRUE</MPG MDC Medical>
Hope i am clear this time
|
|

February 22nd, 2011, 01:07 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
That 'output' is not valid XML. You cannot have a space in an XML element name.
|
|

February 22nd, 2011, 01:13 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I just removed the spaces as well to make valid xml.
<ComplaintOriginator>super13</ComplaintOriginator>
<MPGComplaintReceiptDate>2010-08-20T05:00:00Z</MPGComplaintReceiptDate>
<MPGComplaintReopenCount>0</MPGComplaintReopenCount>
<MPGComplaintTPMNotificationDate></MPGComplaintTPMNotificationDate>
<MPGMDCMedical>TRUE</MPGMDCMedical>
|
|

February 22nd, 2011, 01:18 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
So was there an actual question - because I'm afraid I don't see one.
You can use the translate() function with Michael's answer above to solve your problem. If this isn't working then you need to let us know why.
|
|

February 22nd, 2011, 01:25 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am not very clear on translate from the above post. I tried the code, it's hanging in the middle
|
|

February 22nd, 2011, 02:02 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is my initial code, work in progress
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/ematrix/businessObject">
<xsl:element name="{translate(objectType,' ','')}">
<Type>
<xsl:value-of select="objectType"/>
</Type>
<Name>
<xsl:value-of select="objectName"/>
</Name>
<revision>
<xsl:value-of select="objectRevision"/>
</revision>
<vault>
<xsl:value-of select="vaultRef"/>
</vault>
<owner>
<xsl:value-of select="owner/userRef"/>
</owner>
<description>
<xsl:value-of select="description"/>
</description>
<originated>
<xsl:value-of select="creationInfo/datetime"/>
</originated>
<modified>
<xsl:value-of select="modificationInfo/datetime"/>
</modified>
<policy>
<xsl:value-of select="policyRef"/>
</policy>
<xsl:for-each select="attributeList/attribute">
<xsl:call-template name="attribute"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template name="attribute">
<xsl:element name="{name()}">
<xsl:value-of select="*[2]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output not as desired
Code:
<?xml version='1.0' ?>
V6R2009x.HF29(13.0.0.0)2011-02-17T02:41:20ZexporteMatriXML version V6R2008-1.0<ADDComplaint><Type xmlns:xsd="http://www.w3.org/2001/XMLSchema">ADD Complaint</Type><Name xmlns:xsd="http://www.w3.org/2001/XMLSchema">Ticket Eight 00001</Name><revision xmlns:xsd="http://www.w3.org/2001/XMLSchema">1</revision><vault xmlns:xsd="http://www.w3.org/2001/XMLSchema">ADD</vault><owner xmlns:xsd="http://www.w3.org/2001/XMLSchema">ADDsuper3</owner><description xmlns:xsd="http://www.w3.org/2001/XMLSchema"/><originated xmlns:xsd="http://www.w3.org/2001/XMLSchema">2011-02-12T11:13:06Z</originated><modified xmlns:xsd="http://www.w3.org/2001/XMLSchema">2011-02-12T11:13:46Z</modified><policy xmlns:xsd="http://www.w3.org/2001/XMLSchema">MPG Complaint</policy><attribute>ADDsuper3</attribute><attribute/><attribute>4 Low</attribute><attribute>Yes</attribute><attribute/><attribute/><attribute/><attribute/><attribute>US</attribute><attribute/><attribute>2010-08-20T05:00:00Z</attribute>
Making progress, please let me know if you have any inputs
|
|

February 22nd, 2011, 03:03 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
It should be <xsl:element name="{name}"> and not <xsl:element name="{name()}">.
The former picks the text of the child element called 'name' (i.e. <name>) whereas the latter outputs the name of the current element (i.e. <attribute>).
Use the translate function exactly as you have above, i.e. translate(name,' ','').
If you get rid of the xsd namespace declaration as well that will clean up your output.
|
|

February 22nd, 2011, 03:43 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is my updated code.
The attributeList/attribute is working. The next level set is not working. Can someone take a look at it, the for each takes place but not finding the values at each context.
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/ematrix/businessObject">
<xsl:element name="{translate(objectType,' ','')}">
<Type>
<xsl:value-of select="objectType"/>
</Type>
<Name>
<xsl:value-of select="objectName"/>
</Name>
<revision>
<xsl:value-of select="objectRevision"/>
</revision>
<vault>
<xsl:value-of select="vaultRef"/>
</vault>
<owner>
<xsl:value-of select="owner/userRef"/>
</owner>
<description>
<xsl:value-of select="description"/>
</description>
<originated>
<xsl:value-of select="creationInfo/datetime"/>
</originated>
<modified>
<xsl:value-of select="modificationInfo/datetime"/>
</modified>
<policy>
<xsl:value-of select="policyRef"/>
</policy>
<xsl:for-each select="attributeList/attribute">
<xsl:call-template name="attribute"/>
</xsl:for-each>
<states>
<xsl:for-each select="stateList/state">
<xsl:element name="state">
<xsl:element name="name">
<xsl:value-of select="name"/>
</xsl:element>
<xsl:element name="current">
<xsl:value-of select="stateStatus"/>
</xsl:element>
<xsl:element name="actual">
<xsl:value-of select="actualInfo/datetime"/>
</xsl:element>
<xsl:element name="start">
<xsl:value-of select="startDate/datetime"/>
</xsl:element>
<xsl:element name="end">
<xsl:value-of select="endDate/datetime"/>
</xsl:element>
<xsl:element name="duration">
<xsl:value-of select="stateDuration"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</states>
</xsl:element>
</xsl:template>
<xsl:template name="attribute">
<xsl:element name="{translate(name,' ','')}">
<xsl:value-of select="*[2]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
|
|
 |