使用 XSLT 复制部分 xml
Copy partial xml using XSLT
我已经输入 xml 如下所示,并期望输出如下。
输入xml:
<GetItemResponse xmlns="urn:api:pis:BaseComponents">
<Ack>Success</Ack>
<Item>
<AutoPay>false</AutoPay>
<ListingDetails>
<Adult>false</Adult>
<EndingReason>NotAvailable</EndingReason>
</ListingDetails>
<Duration>35</Duration>
<ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>
<ShipToLocations>US</ShipToLocations>
<HideFromSearch>false</HideFromSearch>
</Item>
</GetItemResponse>
输出 xml 应该如下所示:
<ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>
因此我写了 xslt 类似下面的东西,但它不起作用。我正在使用 xslt 2.0。请帮助我完成这个 xslt 转换。
XSLT :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="urn:ebay:apis:eBLBaseComponents">
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<ItemFee>
<product_id></product_id>
<Details>
<xsl:choose>
<xsl:when test="/ns:GetItemResponse/ns:Ack = 'Failure'">
<Ack>Failure</Ack>
<itemid>0</itemid>
<ebay_listing_status>-3</ebay_listing_status>
<status>FALSE</status>
<listed_timestamp>now</listed_timestamp>
<listing_reason>
<xsl:value-of select="/ns:GetItemResponse/ns:Errors[position()=last()]/ns:LongMessage"/>
</listing_reason>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</Details>
</ItemFee>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="ShippingDetails">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果你想简单地提取一个元素并去除命名空间,那么使用它就足够了
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="urn:api:pis:BaseComponents"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/GetItemResponse/Item[1]/ShippingDetails[1]"/>
</xsl:template>
</xsl:stylesheet>
当我将 Saxon 9.7 HE 用于您发布的输入样本时
<?xml version="1.0" encoding="UTF-8"?>
<GetItemResponse xmlns="urn:api:pis:BaseComponents">
<Ack>Success</Ack>
<Item>
<AutoPay>false</AutoPay>
<ListingDetails>
<Adult>false</Adult>
<EndingReason>NotAvailable</EndingReason>
</ListingDetails>
<Duration>35</Duration>
<ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>
<ShipToLocations>US</ShipToLocations>
<HideFromSearch>false</HideFromSearch>
</Item>
</GetItemResponse>
输出是
<?xml version="1.0" encoding="UTF-8"?><ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>
我已经输入 xml 如下所示,并期望输出如下。
输入xml:
<GetItemResponse xmlns="urn:api:pis:BaseComponents">
<Ack>Success</Ack>
<Item>
<AutoPay>false</AutoPay>
<ListingDetails>
<Adult>false</Adult>
<EndingReason>NotAvailable</EndingReason>
</ListingDetails>
<Duration>35</Duration>
<ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>
<ShipToLocations>US</ShipToLocations>
<HideFromSearch>false</HideFromSearch>
</Item>
</GetItemResponse>
输出 xml 应该如下所示:
<ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>
因此我写了 xslt 类似下面的东西,但它不起作用。我正在使用 xslt 2.0。请帮助我完成这个 xslt 转换。
XSLT :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="urn:ebay:apis:eBLBaseComponents">
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<ItemFee>
<product_id></product_id>
<Details>
<xsl:choose>
<xsl:when test="/ns:GetItemResponse/ns:Ack = 'Failure'">
<Ack>Failure</Ack>
<itemid>0</itemid>
<ebay_listing_status>-3</ebay_listing_status>
<status>FALSE</status>
<listed_timestamp>now</listed_timestamp>
<listing_reason>
<xsl:value-of select="/ns:GetItemResponse/ns:Errors[position()=last()]/ns:LongMessage"/>
</listing_reason>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</Details>
</ItemFee>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="ShippingDetails">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果你想简单地提取一个元素并去除命名空间,那么使用它就足够了
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="urn:api:pis:BaseComponents"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/GetItemResponse/Item[1]/ShippingDetails[1]"/>
</xsl:template>
</xsl:stylesheet>
当我将 Saxon 9.7 HE 用于您发布的输入样本时
<?xml version="1.0" encoding="UTF-8"?>
<GetItemResponse xmlns="urn:api:pis:BaseComponents">
<Ack>Success</Ack>
<Item>
<AutoPay>false</AutoPay>
<ListingDetails>
<Adult>false</Adult>
<EndingReason>NotAvailable</EndingReason>
</ListingDetails>
<Duration>35</Duration>
<ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>
<ShipToLocations>US</ShipToLocations>
<HideFromSearch>false</HideFromSearch>
</Item>
</GetItemResponse>
输出是
<?xml version="1.0" encoding="UTF-8"?><ShippingDetails>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<CalculatedShippingRate>
<WeightMajor measurementSystem="English" unit="lbs">0</WeightMajor>
<WeightMinor measurementSystem="English" unit="oz">0</WeightMinor>
</CalculatedShippingRate>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ShippingServiceOptions>
<ShippingService>ShippingMethodStandard</ShippingService>
<ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
<ShippingServiceAdditionalCost currencyID="USD">0.0</ShippingServiceAdditionalCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ExpeditedService>false</ExpeditedService>
<ShippingTimeMin>1</ShippingTimeMin>
<ShippingTimeMax>6</ShippingTimeMax>
<FreeShipping>true</FreeShipping>
</ShippingServiceOptions>
<ExcludeShipToLocation>PO Box</ExcludeShipToLocation>
<SellerExcludeShipToLocationsPreference>true</SellerExcludeShipToLocationsPreference>
</ShippingDetails>