XSLT - Mismatched tags
I'm trying to use XLST with this XML:
<?xml version="1.0"?>
<ArrayO开发者_如何转开发fDynamicData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DynamicData>
<item>
<name>Name</name>
<value xsi:type="xsd:int">0</value>
</item>
<item>
<name>Value</name>
<value xsi:type="xsd:long">1</value>
</item>
</DynamicData>
</ArrayOfDynamicData>
The XLST i have at the minute is:
<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"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs xdt err fn">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<graph showNames='1' caption='##TITLE HERE##' decimalPrecision='0' >
<xsl:for-each select="ArrayOfDynamicData/DynamicData/item">
<xsl:if test="name='Name'">
<set>
<name><xsl:value-of select='value' /></name>
</xsl:if>
<xsl:if test="name='Value'">
<value><xsl:value-of select='value' /></value>
</set>
</xsl:if>
</xsl:for-each>
</graph>
</xsl:template>
</xsl:stylesheet>
Now in my head this should work, but i get an error due to the mismatched tags. But i need data from two blocks to be placed within one block and i'm not sure how i can achieve this.
Thanks, james.
Edit:
I'm trying to achieve this:
<set>
<name>0</name>
<value>2</value>
</set>
If i put the <set>
tags outside the if's, I get:
<set>
<name>0</name>
</set>
<set>
<value>2</value>
</set>
This can be done in a very simple way:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/>
<xsl:template match="/">
<graph showNames='1' caption='##TITLE HERE##' decimalPrecision='0'>
<set>
<xsl:for-each select=
"ArrayOfDynamicData/DynamicData/item[name='Name']">
<name>
<xsl:value-of select='value' />
</name>
<value>
<xsl:value-of select=
"following-sibling::item[name = 'Value'][1]/value" />
</value>
</xsl:for-each>
</set>
</graph>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<ArrayOfDynamicData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DynamicData>
<item>
<name>Name</name>
<value xsi:type="xsd:int">0</value>
</item>
<item>
<name>Value</name>
<value xsi:type="xsd:long">1</value>
</item>
</DynamicData>
</ArrayOfDynamicData>
the wanted, correct result is produced:
<graph showNames="1" caption="##TITLE HERE##" decimalPrecision="0">
<set>
<name>0</name>
<value>1</value>
</set>
</graph>
You're actually treating both item
elements independently doing it this way, which means they won't ever get combined into a single set
element in the output. If you can guarantee that a value always follows a name, then you can do this:
<xsl:for-each select="ArrayOfDynamicData/DynamicData/item[name='Name']">
<set>
<name><xsl:value-of select="value" /></name>
<value><xsl:value-of select="following-sibling::item[1]/value"></value>
</set>
</xsl:for-each>
There is an </xsl:if>
missing here:
<xsl:if test="name='Value'">
<value><xsl:value-of select='value' /></value>
</set>
Edit
At closer inspection, the <set>
tag falls out of the structure. Take a good look at the entire body of <xsl:for-each>
:
<xsl:if test="name='Name'">
<set> <!-- Start tag here. -->
<name><xsl:value-of select='value' /></name>
<!-- no end tag -->
</xsl:if>
<xsl:if test="name='Value'">
<!-- No set start tag -->
<value><xsl:value-of select='value' /></value>
</set> <!-- End tag here -->
</xsl:if>
精彩评论