我正在尝试从NO
元素中删除字符cbc:ID
,但仅在找到这些字符的文件中删除。
<cbc:ID>NO123456789</cbc:ID>
应成为:
<cbc:ID>123456789</cbc:ID>
我已经取得了一些进步,但是遇到了障碍,可以看到我做错了什么。 Notepad ++ XML Tools插件抱怨XSL无效。
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cac:PaymentMeans>
<cbc:PaymentMeansCode>31</cbc:PaymentMeansCode>
<cac:PayeeFinancialAccount>
<cbc:ID>NO60210517971</cbc:ID>
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
</Invoice>
XSL文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="cac:PaymentMeans">
<Invoice version="1.0"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cac:PaymentMeans>
<cac:PayeeFinancialAccount>
<cbc:ID>
<xsl:variable name="accountnumber" select="cac:PayeeFinancialAccount/cbc:ID"/>
<xsl:choose>
<xsl:when test="contains($accountnumber, 'NO'">
<xsl:value-of select="translate($accountnumber, 'NO', '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$accountnumber"/>
</xsl:otherwise>
</xsl:choose>
</cbc:ID>
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
</Invoice>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
我对您的要求“我正在尝试从cbc:ID元素中删除字符NO ...”的解释是以下样式表解决方案。 只需替换一次,其余的可以通过复制模板处理:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="//cbc:ID" priority="2">
<xsl:copy>
<xsl:value-of select="replace(.,'NO','')"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
以下是将xslt应用于xml的结果:
<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cac:PaymentMeans>
<cbc:PaymentMeansCode>31</cbc:PaymentMeansCode>
<cac:PayeeFinancialAccount>
<cbc:ID>60210517971</cbc:ID>
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
</Invoice>