나는이 XML이 :XSLT - 모든 다른 사람을 버리고 주어진 요소를 유지, XSLT를 사용하여 XML 문서를 필터링하는 방법
<?xml version="1.0" encoding="utf-8"?>
<xml>
<head>
<name>This is my XML</name>
<version>This is my XML</version>
</head>
<body>
<item id="SH2-99435">
<properties>
<property id="69">
<name>This is a property</name>
<amount>54.13</amount>
<estructure id="IZ4">
<name>caterpillar</name>
<location><zipCode>02210</zipCode><street>South Station</street></location>
</estructure>
</property>
<features id="ABC3">
<feature>If it works, a bug is another feature.</feature>
<feature>feature!!</feature>
</features>
</properties>
<coding>
<codename>Silent Hill 2</codename>
<developer>Team Silent</developer>
<publisher>Konami</publisher>
</coding>
</item>
<item id="SH3-4498">
<text value="it has values like the other item"/>
</item>
<item id="MGS-2">
<text value="it has values like the other item"/>
</item>
</body>
</xml>
을 그리고 난이 달성하고자하는 :
<?xml version="1.0" encoding="utf-8"?>
<xml>
<head>
<name>This is my XML</name>
<version>This is my XML</version>
</head>
<body>
<item>
<properties>
<property id="69">
<amount>54.13</amount>
<estructure id="IZ4">
<name>caterpillar</name>
<location><zipCode>02210</zipCode><street>South Station</street></location>
</estructure>
</property>
</properties>
</item>
<item id="SH3-4498">
<text value="it should have properties and the selected sons"/>
</item>
<item id="MGS-2" >
<text value="it should have properties and the selected sons"/>
</item>
</body>
</xml>
내가 뭔가를 다음과 같이 정확히 필터링합니다.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xml/body/item/coding" />
<xsl:template match="xml/body/item/properties/features" />
<xsl:template match="xml/body/item/properties/property/name" />
</xsl:stylesheet>
나는 10 개의 파일이 실행 중임을 알게되었습니다. 각각 다른 태그를 가진 필터; 하지만 새로운 태그가 오면 필요한 파일에만 태그를 추가하는 대신 불필요한 태그를 제외하기 위해 10 개의 파일을 변경해야합니다. 예를 들어, 다른 파일에는 코딩 만 포함됩니다.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<ns:WhiteList>
<name>amount</name>
<name>estructure</name>
</ns:WhiteList>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template
match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]" />
</xsl:stylesheet>
을하지만 estructure의 자식을 복사하지 않습니다 :
나는이 함께했습니다. 내가 할 수있는 것을 알고 있습니까?감사합니다.
업데이트.이 방법으로, 그리고 다른 방법으로, 그리고 더 기술적 인 질문을하기 위해 정당성을 추가했습니다.
처음 XSL이 나에게 충분히 명확 해 보입니다! 비즈니스 규칙이 특정 노드를 제외한 모든 것을 복사하는 것이면 그것은 길일 것입니다. 하나의 불일치가 있지만, 초기 XSL에서는 ** estructure ** node를 제외하고 있지만 이것은 예상 출력에 여전히 존재합니다. –
@TimC 죄송합니다, 나는 구조를 포함하도록 첫 번째 xslt를 수정했습니다. – StrayChild01