xml - 如果标记不存在,则使用 XSLT 将 XML 标记添加到 SOAP 消息
问题描述
我对 XSLT 很陌生,不知道如何为我想要的转换找到解决方案。我们收到一个 SOAP 请求,需要检查某个标签(Steps)是否存在。如果不存在,我想将其添加到 SOAP 消息中。
我们在 Azure API 管理上运行该接口,它仅在 1.0 版中允许 XSLT。
XML 示例
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ProductionToSupply xmlns="http://somenamespace.com">
<ProductionDataList>
<ProductionData>
<Material_Number>5</Material_Number>
<Doc_Number>1234</Doc_Number>
<Description>abcde</Description>
</ProductionData>
</ProductionDataList>
</ProductionToSupply>
</soap:Body>
</soap:Envelope>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://somenamespace.com" exclude-result-prefixes="ns1">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="soap:Envelope/soap:Body/ns1:ProductionToSupply/ns1:ProductionDataList/ns1:ProductionData/ns1:Doc_Number">
<xsl:if test="not(soap:Envelope/soap:Body/ns1:ProductionToSupply/ns1:ProductionDataList/ns1:ProductionData/ns1:Steps)">
<Steps xmlns="http://somenamespace.com">NA</Steps>
</xsl:if>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
期望的输出
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ProductionToSupply xmlns="http://somenamespace.com">
<ProductionDataList>
<ProductionData>
<Material_Number>5</Material_Number>
<Doc_Number>1234</Doc_Number>
<Description>abcde</Description>
<Steps>NA</Steps>
</ProductionData>
</ProductionDataList>
</ProductionToSupply>
</soap:Body>
</soap:Envelope>
如果“ProductionData”组中不存在标签“Steps”,则预期的结果是仅添加标签,但我不知道如何设置适当的匹配模式。
提前感谢您的帮助
解决方案
如果您只想匹配ProductionData
没有Steps
元素的组,则模板匹配将是这个
<xsl:template match="ns1:ProductionData[not(ns1:Steps)]">
然后您可以复制ProductionData
元素及其子节点,并同时滑入Steps
元素
试试这个 XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://somenamespace.com" exclude-result-prefixes="ns1">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:ProductionData[not(ns1:Steps)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<Steps xmlns="http://somenamespace.com">NA</Steps>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这将添加Steps
作为最后一个孩子。如果你想在之后立即Doc_Number
使用,用这个替换模板
<xsl:template match="ns1:ProductionData[not(ns1:Steps)]/ns1:Doc_Number">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<Steps xmlns="http://somenamespace.com">NA</Steps>
</xsl:template>
推荐阅读
- gpflow - 泊松似然的 GPflow 预测均值/方差
- reactjs - 我应该使用什么 babel 插件来正确工作 react-dnd?
- spring-boot - 无法在 java 中使用 DocumentClient 从 cosmos 中删除文档
- c# - 如何显示一个消息框,询问用户是否真的要退出 cmd 窗口?
- android - 导航组件问题和 TextInputLayout 错误
- google-cloud-data-fusion - 无法将 Excel 文件从 Google Cloud Storage 加载到 Google Data Fusion
- sqlite - 使用子表从父表中获取值
- python - 根据 Firestore 更改触发 Cloud Function
- angular - 为什么 Stripe 付款请求按钮会出现错误?
- javascript - TypeScript 中泛型的行为