我想在我的身份转换中添加新节点,因此输出也将包含新节点。
这是我的身份转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</xsl:template>
<xsl:template match="FIELD1"></xsl:template>
<xsl:template match="ROWSET">
<listing>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</listing>
</xsl:template>
<xsl:template match="ROW">
<listing>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</listing>
</xsl:template>
<xsl:template match="FIELD2">
<title lang="en_US">
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</title>"
</xsl:template>
<xsl:template match="FIELD5">
<content lang="en_US">
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</content>
</xsl:template>
<xsl:template match="FIELD6">
<price>
<!-- output Salary as custom text -->
<xsl:text>Salary is </xsl:text>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</price>
</xsl:template>
<xsl:template match="FIELD4">
<region>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</region>
</xsl:template>
<xsl:template match="FIELD3">
<custom name="client">
<xsl:text>Company name is </xsl:text>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</custom>
</xsl:template>
<xsl:template match="FIELD7">
<custom name="link">
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</custom>
</xsl:template>
<!-- output result -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我希望我的身份转换为复制的示例XML是:
<?xml version="1.0" encoding="utf-8" ?>
<listings>
<listing>
<title lang="en_US"><![CDATA[Title in USmana1]]></title>
<content lang="en_US"><![CDATA[Lor4em ipsum dolor sit amet, consectetur adipiscing elit. Donec id nulla dolor. Ut aliquet, mauris id molestie porttitor, dui lorem pretium sem, quis vestibulum orci purus ultrices lacus. Nulla accumsan tortor porta nibh consequat rhoncus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Morbi elementum venenatis ante eu porta. Curabitur ultricies nunc vel elit rutrum scelerisque. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse rutrum lectus quis sem malesuada nec placerat sapien malesuada.]]></content>
<category lang="en_US">Auto</category>
<contactemail>email@yahoo.com</contactemail>
<contactname>Mr. False Name1</contactname>
<price>123.45</price>
<currency>USD</currency>
<city_area>07841454774</city_area>
<city>Arizona</city>
<region>Arizona</region>
<countryId>US</countryId>
<country>United States</country>
<custom name="client">Companie</custom>
<custom name="tip">Vanzare</custom>
<image>http://conejo.me/cats/153.jpg</image>
<image>http://conejo.me/cats/598.jpg</image>
<image>http://conejo.me/cats/1908.jpg</image>
<datetime>2013-03-08 12:34:56</datetime>
</listing>
<listing>
<title lang="en_US"><![CDATA[Title in English 2]]></title>
<content lang="en_US"><![CDATA[Lor3em ipsum dolor sit amet, consectetur adipiscing elit. Donec id nulla dolor. Ut aliquet, mauris id molestie porttitor, dui lorem pretium sem, quis vestibulum orci purus ultrices lacus. Nulla accumsan tortor porta nibh consequat rhoncus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Morbi elementum venenatis ante eu porta. Curabitur ultricies nunc vel elit rutrum scelerisque. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse rutrum lectus quis sem malesuada nec placerat sapien malesuada.]]></content>
<category lang="en_US">Real-Estate</category>
<contactemail>your.false123@email.com</contactemail>
<contactname>Mrs. True Name 123 1</contactname>
<price>98.76</price>
<currency>USD</currency>
<city_area>0845744774</city_area>
<city>Bucks</city>
<region>Arizona</region>
<countryId>US</countryId>
<country>United States</country>
<custom name="client">Companie</custom>
<custom name="tip">Inchiriere</custom>
<image>http://conejo.me/cats/13.jpg</image>
<image>http://conejo.me/cats/598.jpg</image>
<image>http://conejo.me/cats/1908.jpg</image>
<datetime>2012-08-03 12:34:56</datetime>
</listing>
如您所见,我的变换并未包含类别等节点。如何将非现有节点(即,在需要转换的XML中不匹配)添加到我的身份转换中,以便我可以复制上面粘贴的示例XML的结构?
---附加信息---
原始XML的片段是:
<?xml version="1.0"?>
<ROWSET>
<ROW>
<FIELD1></FIELD1>
<FIELD2>job_title</FIELD2>
<FIELD3>company</FIELD3>
<FIELD4>location</FIELD4>
<FIELD5>description</FIELD5>
<FIELD6>salary</FIELD6>
<FIELD7>link</FIELD7>
</ROW>
<ROW>
<FIELD1>1</FIELD1>
<FIELD2>ADMINSTRATIVE ASSISTANT</FIELD2>
<FIELD3> Company name [here]</FIELD3>
<FIELD4>Georgia</FIELD4>
<FIELD5>A great place to work for…</FIELD5>
<FIELD6>$53,000 </FIELD6>
<FIELD7>[Link](blah.com)</FIELD7>
</ROW>
<ROW>
<FIELD1>2</FIELD1>
<FIELD2>OFFICE SPECIALIST</FIELD2>
<FIELD3> Company name [here]</FIELD3>
<FIELD4>Georgia</FIELD4>
<FIELD5>Provide customer service to patient…</FIELD5>
<FIELD6>$43,000 </FIELD6>
<FIELD7>[Link](blah.com)</FIELD7>
</ROW>
</ROWSET>
----更多细节:----
我尝试做的是将原始XML转换为我之前发布的示例XML。尽管如此,原始XML没有类别,电子邮件等节点。我想从示例XML添加类似的节点,所以我可以有一个XML页面,我可以将其提供给插件,该插件将数据插入到我的数据库中。我用于某个软件的插件是如此具体,以至于如果XML结构与示例XML的结构不相似,则插件将无法将数据插入数据库。
答案 0 :(得分:0)
如果输出中元素的顺序不重要,您只需在模板中输出与ROW
元素匹配的额外节点
<xsl:template match="ROW">
<listing>
<xsl:apply-templates select="@*|node()" />
<category>Default Category</category>
<contactemail>Default Email</contactemail>
<contactname>Default Contact</contactname>
</listing>
</xsl:template>
但是,如果顺序很重要,并且您确实希望在特定位置输出额外节点,则可以通过显式选择现有节点并在中间输出额外节点来完成此操作,如下所示:
<xsl:template match="ROW">
<listing>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="FIELD1|FIELD2" />
<category>Default Category</category>
<contactemail>Default Email</contactemail>
<contactname>Default Contact</contactname>
<xsl:apply-templates select="FIELD3|FIELD4|FIELD5|FIELD6|FIELD7" />
</listing>
</xsl:template>