我正试图逃避子节点和字段的字符。我想保留< >和“其他父节点。这可以通过XSL吗?当我正在测试时,所有字符都被转义,包括<>”父节点。
我在Chris Scott的照顾前2周从stackoverflow获得了这段代码。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<xsl:template match="/">
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"><ns0:Message1>
<xsl:apply-templates/>
</ns0:Message1></ns0:Messages>
</xsl:template>
<xsl:template match="*">
<xsl:text disable-output-escaping="no"><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:apply-templates select="@*"/>
<xsl:text disable-output-escaping="no">></xsl:text>
<xsl:apply-templates select="node()"/>
<xsl:text disable-output-escaping="no"></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="no">></xsl:text>
</xsl:template>
<xsl:template match="@*">
<xsl:text disable-output-escaping="no"> </xsl:text>
<xsl:value-of select="name()" disable-output-escaping="no"/>
<xsl:text disable-output-escaping="yes">=&quot;</xsl:text>
<xsl:value-of select="." disable-output-escaping="no"/>
<xsl:text disable-output-escaping="yes">&quot;</xsl:text>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="ns0:Messages | ns0:Message1">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
请参阅以下源代码示例:
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<Information>
<Contact>
<Name type="Data">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Title>Mr.</Title>
</Name>
<MobileNumber>0123456789</MobileNumber>
<Email>johndoe@coldmail.com</Email>
<Address>Antartica</Address>
</Contact>
<Contact>
<Name type="Data">
<FirstName>Mara</FirstName>
<LastName>Sov</LastName>
<Title>Queen</Title>
</Name>
<MobileNumber>9999999999</MobileNumber>
<Email>marasov@reef.com</Email>
<Address>Reef</Address>
</Contact>
<Department>HR</Department>
</Information>
<Information>
<Contact>
<Name type="Data">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Title>Mr.</Title>
</Name>
<MobileNumber>0123456789</MobileNumber>
<Email>johndoe@coldmail.com</Email>
<Address>Antartica</Address>
</Contact>
<Contact>
<Name type="Data">
<FirstName>Mara</FirstName>
<LastName>Sov</LastName>
<Title>Queen</Title>
</Name>
<MobileNumber>9999999999</MobileNumber>
<Email>marasov@reef.com</Email>
<Address>Reef</Address>
</Contact>
<Department>HR</Department>
</Information>
</ns0:Message1>
</ns0:Messages>
请参阅下面的目标示例:
<ns0:Messages>
<ns0:Message1>
<Information>
<Contact>
<Name type="Data">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Title>Mr.</Title>
</Name>
<MobileNumber>0123456789</MobileNumber>
<Email>johndoe@coldmail.com</Email>
<Address>Antartica</Address>
</Contact>
<Contact>
<Name type="Data">
<FirstName>Mara</FirstName>
<LastName>Sov</LastName>
<Title>Queen</Title>
</Name>
<MobileNumber>9999999999</MobileNumber>
<Email>marasov@reef.com</Email>
<Address>Reef</Address>
</Contact>
<Department>HR</Department>
</Information>
<Information>
<Contact>
<Name type="Data">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Title>Mr.</Title>
</Name>
<MobileNumber>0123456789</MobileNumber>
<Email>johndoe@coldmail.com</Email>
<Address>Antartica</Address>
</Contact>
<Contact>
<Name type="Data">
<FirstName>Mara</FirstName>
<LastName>Sov</LastName>
<Title>Queen</Title>
</Name>
<MobileNumber>9999999999</MobileNumber>
<Email>marasov@reef.com</Email>
<Address>Reef</Address>
</Contact>
<Department>HR</Department>
</Information>
</ns0:Messages>
</ns0:Message1>
目前,我的XSL映射中的输出并不是我所期望的。
<ns0:Messages>
<ns0:Message1>
<Information>
<Contact>
<Name type="Data">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Title>Mr.</Title>
</Name>
<MobileNumber>0123456789</MobileNumber>
<Email>johndoe@coldmail.com</Email>
<Address>Antartica</Address>
</Contact>
<Contact>
<Name type="Data">
<FirstName>Mara</FirstName>
<LastName>Sov</LastName>
<Title>Queen</Title>
</Name>
<MobileNumber>9999999999</MobileNumber>
<Email>marasov@reef.com</Email>
<Address>Reef</Address>
</Contact>
<Department>HR</Department>
</Information>
<Information>
<Contact>
<Name type="Data">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Title>Mr.</Title>
</Name>
<MobileNumber>0123456789</MobileNumber>
<Email>johndoe@coldmail.com</Email>
<Address>Antartica</Address>
</Contact>
<Contact>
<Name type="Data">
<FirstName>Mara</FirstName>
<LastName>Sov</LastName>
<Title>Queen</Title>
</Name>
<MobileNumber>9999999999</MobileNumber>
<Email>marasov@reef.com</Email>
<Address>Reef</Address>
</Contact>
<Department>HR</Department>
</Information>
</ns0:Messages>
</ns0:Message1>
信息节点及其子节点部门也都被转义。我只需要转义其所有子节点的联系人段。
谢谢先生们!
答案 0 :(得分:0)
以下是样式表的更正版本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<xsl:template match="*">
<xsl:text disable-output-escaping="no"><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:apply-templates select="@*"/>
<xsl:text disable-output-escaping="no">></xsl:text>
<xsl:apply-templates select="node()"/>
<xsl:text disable-output-escaping="no"></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="no">></xsl:text>
</xsl:template>
<xsl:template match="@*">
<xsl:text disable-output-escaping="no"> </xsl:text>
<xsl:value-of select="name()" disable-output-escaping="no"/>
<xsl:text disable-output-escaping="yes">=&quot;</xsl:text>
<xsl:value-of select="." disable-output-escaping="no"/>
<xsl:text disable-output-escaping="yes">&quot;</xsl:text>
</xsl:template>
<xsl:template match="ns0:Messages | ns0:Message1 |Information | Department">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
模板<xsl:template match="text()">
并没有用,所以它已被移除。
这是我到目前为止所获得的:
<?xml version="1.0" encoding="UTF-8"?><ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<Information>
<Contact>
<Name type="Data">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Title>Mr.</Title>
</Name>
<MobileNumber>0123456789</MobileNumber>
<Email>johndoe@coldmail.com</Email>
<Address>Antartica</Address>
</Contact>
<Contact>
<Name type="Data">
<FirstName>Mara</FirstName>
<LastName>Sov</LastName>
<Title>Queen</Title>
</Name>
<MobileNumber>9999999999</MobileNumber>
<Email>marasov@reef.com</Email>
<Address>Reef</Address>
</Contact>
<Department>HR</Department>
</Information>
<!-- More elements ... -->
</ns0:Message1>
</ns0:Messages>
您可以在此处查看:http://xsltransform.net/gWvjQeV