问题
Consider my input xml file as below
<?xml version="1.0" encoding="UTF-8"?>
<com xsi:schemaLocation="http://do.way.com/sales/Amb
http://do.way.com/temp/sales/ale/ax.xsd"
xmlns:w="http://do.way.com/sales/W"
xmlns="http://do.way.com/sales/Amb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wire="http://do.way.com/sales/Wire">
<content>
<wire:wire>
<wire:sI>
<w:aH>
<text>Ccc <dynamic name="C_R_N"/>: More</text>
<w:contact value="false"/>
</w:aH>
<w:page>
<nL>Please call us</nL>
</w:page>
<w:body>
<text> C R N: <strong>
<dynamic name="C_R_N"/></strong>
</text>
</w:body>
<w:body>
<text>
RE: <dynamic name="D_C_P"/> <dynamic name="M_C_O"/>
</text>
</w:body>
<w:body>
<text>
In order <strong>
<dynamic name="M_D_D"/>
</strong>, we need some information.
</text>
</w:body>
<w:body>
<text>
<strong>
Please call us <dynamic name="C_P_D_N"/>.
</strong> Our hours <ul class="nested">
<li>
Monday - Friday,
<dynamic name="DST_M_F_S_Hour"/> am -
<dynamic name="DST_M_F_E_Hour"/> pm; Saturday,
<dynamic name="DST_M_F_S_Hour"/> am -
<dynamic name="DST_M_F_E_Hour"/> pm.
</li>
</ul>
</text>
</w:body>
<w:body>
<text>
Thank you for your action. .
</text>
</w:body>
<w:body>
<text>
<dynamic name="C_D_N"/>
</text>
</w:body>
<w:TextAndImage>
<text>
<dynamic name="C_S_N_D"/>
</text>
</w:TextAndImage>
<w:dynamicNames>
<w:dynamicName value="LOW"/>
<w:dynamicName value="Env"/>
</w:dynamicNames>
</wire:sI>
</wire:wire>
</content>
</com>
the required xsl file which transforms the above input xml file refers to a Properties.xml file which has the respective transformed tags(values) for the input xml tag elements(keys) given below Properties.xml
<?xml version="1.0" encoding="utf-8"?>
<Properties>
<Property value="w:aH">aHe</Property>
<Property value="w:contact">contact1</Property>
<Property value="w:page">pageH</Property>
<Property value="w:body">bodyP</Property>
<Property value="w:TextAndImage">textAndImage1</Property>
<Property value="w:dynamicNames">dynamicNames1</Property>
<Property value="w:dynamicName">dynamicValName</Property>
</Properties>
and my transformed xml looks like
<Content xsi:schemaLocation="id:d1234 http://abc:10/w/g/B/System/abc.xsd"
xmlns="id:d1234" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<aHe>
<text>Ccc
<dynamic name="C_R_N" xmlns="http://www.w3.org/1999/xhtml" >
C_R_N
</dynamic>
: More
</text>
<contact1>false</contact1>
</aHe>
<pageH>
<nL>Please call us</nL>
</pageH>
<bodyP>
<text> C R N: <strong>
<dynamic name="C_R_N" xmlns="http://www.w3.org/1999/xhtml">
C_R_N
</dynamic>
</strong>
</text>
</bodyP>
<bodyP>
<text>
RE: <dynamic name="D_C_P" xmlns="http://www.w3.org/1999/xhtml">
D_C_P</dynamic>
<dynamic name="M_C_O" xmlns="http://www.w3.org/1999/xhtml">M_C_O</dynamic>
</text>
</bodyP>
<bodyP>
<text>
In order <strong>
<dynamic name="M_D_D" xmlns="http://www.w3.org/1999/xhtml"> M_D_D </dynamic>
</strong>, we need some information.
</text>
</bodyP>
<bodyP>
<text> Please call us
<strong>
<dynamic name="C_P_D_N" xmlns="http://www.w3.org/1999/xhtml">C_P_D_N</dynamic>.
</strong> Our hours <ul class="nested">
<li>
Monday - Friday, <dynamic name="DST_M_F_S_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_S_Hour</dynamic> am - <dynamic name="DST_M_F_E_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_E_Hour</dynamic> pm; Saturday, <dynamic name="DST_M_F_S_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_S_Hour</dynamic> am - <dynamic name="DST_M_F_E_Hour" xmlns="http://www.w3.org/1999/xhtml">
DST_M_F_E_Hour</dynamic>pm.
</li>
</ul>
</text>
</bodyP>
<bodyP>
<text>
Thank you for your action. .
</text>
</bodyP>
<bodyP>
<text>
<dynamic name="C_D_N" xmlns="http://www.w3.org/1999/xhtml">
C_D_N </dynamic>
</text>
</bodyP>
<textAndImage>
<text>
<dynamic name="C_S_N_D" xmlns="http://www.w3.org/1999/xhtml">
C_S_N_D </dynamic>
</text>
</textAndImage>
<dynamicNames1>
<dynamicValName>LOW</dynamicValName>
<dynamicValName>Env</dynamicValName>
</dynamicNames1>
</Content>
Can anyone provide me with the xsl file which meets my requirement.
回答1:
It looks like you don't need the Properties file as you are only getting rid of namespaces which can be done much simpler:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
The rest of what you want to achieve is probably plain xslt stuff.
EDIT
Update including stuff that will get you much closer to your goal
<xsl:template match="/">
<Content>
<xsl:apply-templates/>
</Content>
</xsl:template>
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
<xsl:template match="* [local-name() != 'com' and local-name() != 'content' and local-name() != 'wire' and local-name() != 'sI']">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<xsl:template match="* [local-name() = 'dynamic']">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | @*"/>
<xsl:value-of select="@name"/>
</xsl:element>
</xsl:template>
<xsl:template match="* [local-name() = 'contact' or local-name()='dynamicName']">
<xsl:element name="{local-name()}">
<xsl:value-of select="@value"/>
</xsl:element>
</xsl:template>
The result is
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<aH>
<text>Ccc <dynamic name="C_R_N">C_R_N</dynamic>: More</text>
<contact>false</contact>
</aH>
<page>
<nL>Please call us</nL>
</page>
<body>
<text> C R N: <strong>
<dynamic name="C_R_N">C_R_N</dynamic>
</strong></text>
</body>
<body>
<text>
RE: <dynamic name="D_C_P">D_C_P</dynamic><dynamic name="M_C_O">M_C_O</dynamic></text>
</body>
<body>
<text>
In order <strong>
<dynamic name="M_D_D">M_D_D</dynamic>
</strong>, we need some information.
</text>
</body>
<body>
<text><strong>
Please call us <dynamic name="C_P_D_N">C_P_D_N</dynamic>.
</strong> Our hours <ul class="nested">
<li>
Monday - Friday,
<dynamic name="DST_M_F_S_Hour">DST_M_F_S_Hour</dynamic> am -
<dynamic name="DST_M_F_E_Hour">DST_M_F_E_Hour</dynamic> pm; Saturday,
<dynamic name="DST_M_F_S_Hour">DST_M_F_S_Hour</dynamic> am -
<dynamic name="DST_M_F_E_Hour">DST_M_F_E_Hour</dynamic> pm.
</li>
</ul></text>
</body>
<body>
<text>
Thank you for your action. .
</text>
</body>
<body>
<text>
<dynamic name="C_D_N">C_D_N</dynamic>
</text>
</body>
<TextAndImage>
<text>
<dynamic name="C_S_N_D">C_S_N_D</dynamic>
</text>
</TextAndImage>
<dynamicNames>
<dynamicName>LOW</dynamicName>
<dynamicName>Env</dynamicName>
</dynamicNames>
</Content>
If, instead, you wish to move all attribute values for attributes dynamic/@name and otherElement/@value to the element body, then the following will do:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Content>
<xsl:apply-templates/>
</Content>
</xsl:template>
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
<xsl:template match="* [local-name() != 'com' and local-name() != 'content' and local-name() != 'wire' and local-name() != 'sI']">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<xsl:template match="* [local-name() = 'dynamic']">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | @*[local-name !='name']"/>
<xsl:value-of select="@name"/>
</xsl:element>
</xsl:template>
<xsl:template match="* [local-name() != 'dynamic' and @value]">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | @*[local-name !='value']"/>
<xsl:value-of select="@value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
with result
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<aH>
<text>Ccc <dynamic>C_R_N</dynamic>: More</text>
<contact>false</contact>
</aH>
<page>
<nL>Please call us</nL>
</page>
<body>
<text> C R N: <strong>
<dynamic>C_R_N</dynamic>
</strong></text>
</body>
<body>
<text>
RE: <dynamic>D_C_P</dynamic><dynamic>M_C_O</dynamic></text>
</body>
<body>
<text>
In order <strong>
<dynamic>M_D_D</dynamic>
</strong>, we need some information.
</text>
</body>
<body>
<text><strong>
Please call us <dynamic>C_P_D_N</dynamic>.
</strong> Our hours <ul class="nested">
<li>
Monday - Friday,
<dynamic>DST_M_F_S_Hour</dynamic> am -
<dynamic>DST_M_F_E_Hour</dynamic> pm; Saturday,
<dynamic>DST_M_F_S_Hour</dynamic> am -
<dynamic>DST_M_F_E_Hour</dynamic> pm.
</li>
</ul></text>
</body>
<body>
<text>
Thank you for your action. .
</text>
</body>
<body>
<text>
<dynamic>C_D_N</dynamic>
</text>
</body>
<TextAndImage>
<text>
<dynamic>C_S_N_D</dynamic>
</text>
</TextAndImage>
<dynamicNames>
<dynamicName>LOW</dynamicName>
<dynamicName>Env</dynamicName>
</dynamicNames>
</Content>
回答2:
I have used the below xslt code by taking help of the code given by DavidCarlisle which partially fulfill the requirement
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="id:d1234"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:w="http://do.way.com/sales/W"
xmlns:wire="http://do.way.com/sales/Wire"
exclude-result-prefixes=" wire w ">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Content xsi:schemaLocation="id:d1234 http://abc:10/w/g/B/System/abc.xsd">
<xsl:apply-templates select="w:sI"/>
</Content>
</xsl:template>
<xsl:template match="*[local-name()!=sI]">
<xsl:variable name="n" select="name()"/>
<xsl:variable name="new">
<xsl:for-each select="document('Properties.xml')">
<xsl:choose>
<xsl:when test="key('n',$n)">
<xsl:value-of select="key('n',$n)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$n"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:element name="{$new}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:key name="n" match="Property" use="@value"/>
<xsl:template match="*[name() = 'dynamic']">
<dynamic xmlns="http://www.w3.org/1999/xhtml">
<xsl:copy-of select="@*"/>
<strong>
<xsl:value-of select="@name"/>
</strong>
</dynamic>
</xsl:template>
</xsl:stylesheet>
the only problem im facing in this is i can't comeup with an xslt code which can take the nodes having 'value' attributes (contact and dynamicName) extract its attribute value and put them under respective tag's (specified in Properties.xml) content. Please go through this xsl file and make necessary changes in it or I would be equally glad if you can come up with a new one.But please make sure of using Properties.xml file.Thanks.
来源:https://stackoverflow.com/questions/10071629/need-to-transform-an-xml-file-into-another-xml-using-xslt-whose-xsl-file-uses-a