I'm in need of a particular solution to my XSLT and I've struggling to figure out how to get it to work. Right now my 开发者_如何学PythonXML is structured like this
<DataX>
<DataY>
<fieldy>A</fieldy>
<fieldx>B</fieldx>
<Data1>
<field1>1</field1>
<field2>2</field2>
</Data1>
<Data2>
<field3>3</field3>
<field4>4</field4>
</Data2>
<Data3>
<field5>5</field5>
<field6>6</field6>
</Data3>
</DataY>
</DataX>
What I need to do is append the contents of <DataY> <field1> <field2>
to every instance of the inner data fields. Without altering the original XML
Like this
A B 1 2 A B 3 4 A B 5 6
Like so, I added spaces for clarity.
I've tried some inner for-each statements and few other things. I've not to experience with XSLT but maybe I'm over thinking it. I'm just not sure how to obtain the A B data once a pass has been made using a for-each. Any Thoughts?
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vPrefix">
<xsl:for-each select="/*/DataY/*[not(starts-with(name(),'Data'))]">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/*/*/*[starts-with(name(),'Data')]">
<xsl:value-of select="concat($vPrefix, .)"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
when applied on the provided XML document:
<DataX>
<DataY>
<fieldy>A</fieldy>
<fieldx>B</fieldx>
<Data1>
<field1>1</field1>
<field2>2</field2>
</Data1>
<Data2>
<field3>3</field3>
<field4>4</field4>
</Data2>
<Data3>
<field5>5</field5>
<field6>6</field6>
</Data3>
</DataY>
</DataX>
produces the wanted, correct result (no intervening spaces as they were added in the question "for clarity"):
AB12AB34AB56
You should get started with this transform.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*/*">
<xsl:apply-templates select="*[2]/following-sibling::*" mode="data"/>
</xsl:template>
<xsl:template match="*" mode="data">
<xsl:value-of select="concat(../*[1],../*[2],*[1],*[2])"/>
</xsl:template>
</xsl:stylesheet>
When applied on this input:
<DataX>
<DataY>
<fieldx>A</fieldx>
<fieldy>B</fieldy>
<Data1>
<field1>1</field1>
<field2>2</field2>
</Data1>
<Data2>
<field3>3</field3>
<field4>4</field4>
</Data2>
<Data3>
<field5>5</field5>
<field6>6</field6>
</Data3>
</DataY>
</DataX>
It produces:
AB12AB34AB56
精彩评论