寻求可提取XML元素及值的通用XSLT,以及解析内嵌转义XML字符串的XSLT示例
XSLT Solutions for Your Requirements
Got it, let's break down the two XSLT solutions you're looking for:
1. Generic XSLT to Extract Elements and Their Values from Any XML String
This stylesheet works with any XML input and outputs a structured list of element paths (including namespaces) along with their corresponding text values. It handles nested elements recursively:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" encoding="UTF-8"/> <!-- Match the root node and start processing child elements --> <xsl:template match="/"> <xsl:apply-templates select="*"/> </xsl:template> <!-- Process elements recursively --> <xsl:template match="*"> <!-- Output element path (namespace URI + local name) --> <xsl:text>Element: </xsl:text> <xsl:value-of select="concat(namespace-uri(), ':', local-name())"/> <xsl:text> </xsl:text> <!-- Output element text value if it exists --> <xsl:if test="normalize-space(text()) != ''"> <xsl:text>Value: </xsl:text> <xsl:value-of select="normalize-space(text())"/> <xsl:text> </xsl:text> </xsl:if> <!-- Recursively process child elements --> <xsl:apply-templates select="*"/> </xsl:template> <!-- Ignore text nodes that are just whitespace --> <xsl:template match="text()[normalize-space() = '']"/> </xsl:stylesheet>
How it works:
- Uses XSLT 2.0 features for robust namespace handling (can be adjusted to 1.0 if needed, though namespace support will be more limited)
- Recursively traverses every element in the input XML
- Outputs each element's fully qualified name (namespace URI + local name) and its trimmed text value
- Skips empty whitespace-only text nodes to keep the output clean
2. XSLT to Parse Escaped XML in <ns0:xmlString>
For your specific input XML, this stylesheet will parse the escaped XML content inside <ns0:xmlString> and replace the escaped string with actual XML nodes. Note: This requires an XSLT 2.0+ processor (like Saxon) since it uses the parse-xml() function.
Input XML (your provided structure):
<?xml version="1.0" encoding="UTF-8"?> <app4:App xmlns:app4="http://www.example.org/app4"> <app4:appId>45645dfg</app4:appId> <app4:appType>Insert/Update</app4:appType> <app4:appName>Optin</app4:appName> <app4:source>Store</app4:source> <app4:target>apm</app4:target> <app4:corelationId>564654456</app4:corelationId> <app4:payload> <ns0:xmlString xmlns:ns0="http://www.example.org/ns0"><?xml version="1.0" encoding="UTF-8"?> <app6:consumers xmlns:app6="http://www.example.org/consumer_Notification" xmlns:ns="http://com.equinix.product/consumerxa"> <app6:consumerId>yjtuj</app6:consumerId> <app6:consumerNumber>gdfhfh</app6:consumerNumber> <app6:orderNumber>657467476</app6:orderNumber> <app6:accountNumber>75654757</app6:accountNumber> <app6:accountUcid>6574575747-15C8E09FBEBD</app6:accountUcid> <app6:productName>updateportal</app6:productName> <app6:metro>MB</app6:metro> <app6:country>Japan</app6:country> <app6:consumerType>Patch Panel</app6:consumerType> <app6:consumerPartNumber>654757.COMP</app6:consumerPartNumber> <app6:Status>Active</app6:Status> </app6:consumers></ns0:xmlString> </app4:payload> </app4:App>
XSLT Solution:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:app4="http://www.example.org/app4" xmlns:ns0="http://www.example.org/ns0"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:strip-space elements="*"/> <!-- Identity template: copy all nodes as-is except where overridden --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- Match the ns0:xmlString element and parse its escaped content --> <xsl:template match="ns0:xmlString"> <xsl:copy> <!-- Parse the escaped XML string into a node set --> <xsl:copy-of select="parse-xml(normalize-space(.))/*"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
Output XML (matches your desired result):
<?xml version="1.0" encoding="UTF-8"?> <app4:App xmlns:app4="http://www.example.org/app4"> <app4:appId>45645dfg</app4:appId> <app4:appType>Insert/Update</app4:appType> <app4:appName>Optin</app4:appName> <app4:source>Store</app4:source> <app4:target>apm</app4:target> <app4:corelationId>564654456</app4:corelationId> <app4:payload> <ns0:xmlString xmlns:ns0="http://www.example.org/ns0"> <app6:consumers xmlns:app6="http://www.example.org/consumer_Notification" xmlns:ns="http://com.equinix.product/consumerxa"> <app6:consumerId>yjtuj</app6:consumerId> <app6:consumerNumber>gdfhfh</app6:consumerNumber> <app6:orderNumber>657467476</app6:orderNumber> <app6:accountNumber>75654757</app6:accountNumber> <app6:accountUcid>6574575747-15C8E09FBEBD</app6:accountUcid> <app6:productName>updateportal</app6:productName> <app6:metro>MB</app6:metro> <app6:country>Japan</app6:country> <app6:consumerType>Patch Panel</app6:consumerType> <app6:consumerPartNumber>654757.COMP</app6:consumerPartNumber> <app6:Status>Active</app6:Status> </app6:consumers> </ns0:xmlString> </app4:payload> </app4:App>
Key Notes:
- The identity template ensures all existing nodes are copied unchanged except for
<ns0:xmlString> parse-xml(normalize-space(.))cleans up whitespace in the escaped string and converts it to a proper XML node tree- Make sure to declare all required namespaces (app4, ns0) in your XSLT to avoid namespace errors
内容的提问来源于stack exchange,提问作者mayu
相关产品推荐
相关产品推荐

